有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在ZK中定制组件标签

我需要帮助,我想自定义zk中的标签组件,我需要添加一个属性,当我设置mandatory=“true”时,asterix符号将出现,如果我设置mandatory=“false”,asterix符号将消失,我正在这样尝试:

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;
    }

但是这种情况不起作用,怎么解决呢


共 (1) 个答案

  1. # 1 楼答案

    你可能想要的是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 我的文本框 通用域名格式。实例我的文本框 /zk/textbox。祖尔

    。。。这样你就不需要把定义放在每个。zul文件,你再也不用它了。有关这方面的更多信息,请参见documentation

    当然,你也只能创建一个新标签,等等。但我发现为那些结合了各种组件的工作创建宏组件是一个好主意,因为这样,例如,你也可以自动添加验证等等