我想在 zk 中自定义标签组件,我需要添加一个属性,该属性是强制属性,当我设置强制=“true”时,星号符号将出现,如果我设置强制=“假”,星号符号消失,并且我我正在尝试这样:
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 文件的顶部。有关详细信息,请参阅文档。
当然,你也可以只创建一个新标签等。但我发现为那些组合各种组件的作业创建宏组件是一个很好的想法,因为这样,例如,你还可以自动添加验证等。