Haii,我需要帮助,我想在zk中自定义Label组件,并且在需要设置属性=“ true”时,我会向nedd添加一个属性,该属性将显示星号,如果设置了=“ =”符号消失了,我正在尝试这样:

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

mytextbox
com.example.MyTextbox
/zk/textbox.zul



...因此您无需将定义放在每个使用它的.zul文件的顶部。有关更多信息,请参见documentation

当然,您也只能创建一个新标签,等等。但是我发现为结合各种组件的作业创建MacroComponents是一个好主意,例如,通过这种方式,您还可以自动添加验证等。

07-28 03:12
查看更多