ZK中如何自定义组件标签
How Customize Component Label in ZK
Haii 我需要帮助,我想在 zk 中自定义 Label 组件,我需要添加一个 属性 这是强制性的 属性 当我设置 mandatory="true" 星号符号将出现,如果我设置 mandatory="false" 星号符号消失,我正在尝试这样:
private Label label;
private Label sign;
private String lblValue;
private String REQUIRED_SIGN = " *";
private boolean mandatory;
public SignLabelCustom()
{
label = new Label();
label.setSclass("form-label");
appendChild(label);
sign = new Label();
if(mandatory=true){
sign.setValue(REQUIRED_SIGN);
sign.setStyle("color: red");
appendChild(sign);
}
else{
sign.setValue("");
sign.setStyle("color: red");
removeChild(sign);
}
}
public String getValue() {
return lblValue;
}
public boolean isMandatory() {
return mandatory;
}
public void setMandatory(boolean mandatory) {
this.mandatory = mandatory;
}
public void setValue(String lblValue) {
label.setValue(lblValue);
this.lblValue = lblValue;
}
但是条件不成立,如何解决?
您可能需要的是 HtmlMacroComponent,它结合了标签和文本框...
您从一个 zul 文件开始:
<zk>
<label id="mcLabel"/><textbox id="mcTextbox"/>
</zk>
...并为其创建一个组件...
public class MyTextbox extends HtmlMacroComponent {
@Wire("#mcTextbox")
private Textbox textbox;
@Wire("#mcLabel")
private Label label;
private String caption;
private boolean mandatory;
public MyTextbox() {
compose(); // this wires the whole thing
}
public void setMandatory(final boolean value) {
mandatory = value;
updateCaption();
}
public boolean isMandatory() {
return mandatory;
}
public void setCaption(final String value) {
caption = value;
updateCaption();
}
public String getCaption() {
return caption;
}
protected void updateCaption() {
label.setValue(mandatory ? caption + "*" : caption);
}
public String getValue() {
return textbox.getValue();
}
public void setValue(final String value) {
textbox.setValue(value);
}
}
...现在您可以使用它了,例如在您的 zul 文件顶部定义它...(根据需要调整包和 .zul 名称):
<?component name="mytextbox" macroURI="/zk/textbox.zul" class="com.example.MyTextbox"?>
...所以您可以简单地使用它...
<mytextbox id="name" value="Frank N. Furter" caption="Your name" mandatory="true"/>
稍后你可以为它定义一个语言插件...
我的语言插件
xul/html
我的文本框
com.example.MyTextbox
/zk/textbox.zul
...这样您就无需再将定义放在您使用它的每个 .zul 文件的顶部。有关详细信息,请参阅 documentation。
当然,你也可以只创建一个新的标签等等,但我发现为那些结合了各种组件的工作创建宏组件是一个很好的想法,因为这样,例如,你也可以自动添加验证等
Haii 我需要帮助,我想在 zk 中自定义 Label 组件,我需要添加一个 属性 这是强制性的 属性 当我设置 mandatory="true" 星号符号将出现,如果我设置 mandatory="false" 星号符号消失,我正在尝试这样:
private Label label;
private Label sign;
private String lblValue;
private String REQUIRED_SIGN = " *";
private boolean mandatory;
public SignLabelCustom()
{
label = new Label();
label.setSclass("form-label");
appendChild(label);
sign = new Label();
if(mandatory=true){
sign.setValue(REQUIRED_SIGN);
sign.setStyle("color: red");
appendChild(sign);
}
else{
sign.setValue("");
sign.setStyle("color: red");
removeChild(sign);
}
}
public String getValue() {
return lblValue;
}
public boolean isMandatory() {
return mandatory;
}
public void setMandatory(boolean mandatory) {
this.mandatory = mandatory;
}
public void setValue(String lblValue) {
label.setValue(lblValue);
this.lblValue = lblValue;
}
但是条件不成立,如何解决?
您可能需要的是 HtmlMacroComponent,它结合了标签和文本框...
您从一个 zul 文件开始:
<zk>
<label id="mcLabel"/><textbox id="mcTextbox"/>
</zk>
...并为其创建一个组件...
public class MyTextbox extends HtmlMacroComponent {
@Wire("#mcTextbox")
private Textbox textbox;
@Wire("#mcLabel")
private Label label;
private String caption;
private boolean mandatory;
public MyTextbox() {
compose(); // this wires the whole thing
}
public void setMandatory(final boolean value) {
mandatory = value;
updateCaption();
}
public boolean isMandatory() {
return mandatory;
}
public void setCaption(final String value) {
caption = value;
updateCaption();
}
public String getCaption() {
return caption;
}
protected void updateCaption() {
label.setValue(mandatory ? caption + "*" : caption);
}
public String getValue() {
return textbox.getValue();
}
public void setValue(final String value) {
textbox.setValue(value);
}
}
...现在您可以使用它了,例如在您的 zul 文件顶部定义它...(根据需要调整包和 .zul 名称):
<?component name="mytextbox" macroURI="/zk/textbox.zul" class="com.example.MyTextbox"?>
...所以您可以简单地使用它...
<mytextbox id="name" value="Frank N. Furter" caption="Your name" mandatory="true"/>
稍后你可以为它定义一个语言插件...
我的语言插件 xul/html 我的文本框 com.example.MyTextbox /zk/textbox.zul
...这样您就无需再将定义放在您使用它的每个 .zul 文件的顶部。有关详细信息,请参阅 documentation。
当然,你也可以只创建一个新的标签等等,但我发现为那些结合了各种组件的工作创建宏组件是一个很好的想法,因为这样,例如,你也可以自动添加验证等