【问题标题】:Codename one. Changing a label's text dynamically using revalidate() is not working代号一。使用 revalidate() 动态更改标签的文本不起作用
【发布时间】:2016-02-26 04:36:07
【问题描述】:

我试图在单击按钮时更改标签的文本。所以我使用 setText 更改它然后我调用 revalidate() 但是当我运行它并按下按钮时,标签中的文本不会改变。这是我的代码。 我做错了什么???

public void setUpSignUpDialog() {
    final Dialog signUpDialog = (Dialog) u.createContainer(theme, "SignUpDialog");
    signUpDialog.setDisposeWhenPointerOutOfBounds(true);
    signUpDialog.setTransitionInAnimator(CommonTransitions.createDialogPulsate());
    signUpDialog.setTransitionOutAnimator(CommonTransitions.createDialogPulsate());
    signUpDialog.showPacked(BorderLayout.CENTER, true);

    final TextField emailField;
    TextField passwordField;
    TextField repeatPasswordField;
    Button registerButton;
    final Label errorLabel;

    emailField = (TextField) u.findByName("EmailField", signUpDialog);
    passwordField = (TextField) u.findByName("passwordField", signUpDialog);
    repeatPasswordField = (TextField) u.findByName("RepeatPasswordField", signUpDialog);
    registerButton = (Button) u.findByName("SignUpButton", signUpDialog);
    errorLabel = (Label) u.findByName("ErrorLabel", signUpDialog);

    registerButton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt) {
            if (checkEmailField(emailField.getText())) {

            } else {
                errorLabel.setText("Please enter a valid email address");
                errorLabel.getStyle().setFgColor(ColorUtil.CYAN);
                signUpDialog.animate();
            }
            errorLabel.setText("Please enter a valid email address");
            errorLabel.getStyle().setFgColor(ColorUtil.CYAN);
            signUpDialog.revalidate();
        }
    });
}

添加了signUpDialog的全部代码。我在 if 语句下添加了代码,以防万一没有调用 else。还是不行……

【问题讨论】:

    标签: codenameone


    【解决方案1】:

    替换此代码 signUpDialog.showPacked(BorderLayout.CENTER, true); 使用注册Dilaog.show();它会工作

    public void setUpSignUpDialog() {
        final Dialog signUpDialog = (Dialog) u.createContainer(theme, "SignUpDialog");
        signUpDialog.setDisposeWhenPointerOutOfBounds(true);
        signUpDialog.setTransitionInAnimator(CommonTransitions.createDialogPulsate());
        signUpDialog.setTransitionOutAnimator(CommonTransitions.createDialogPulsate());
      //  signUpDialog.showPacked(BorderLayout.CENTER, true);
    
        final TextField emailField;
        TextField passwordField;
        TextField repeatPasswordField;
        Button registerButton;
        final Label errorLabel;
    
        emailField = (TextField) u.findByName("EmailField", signUpDialog);
        passwordField = (TextField) u.findByName("passwordField", signUpDialog);
        repeatPasswordField = (TextField) u.findByName("RepeatPasswordField", signUpDialog);
        registerButton = (Button) u.findByName("SignUpButton", signUpDialog);
        errorLabel = (Label) u.findByName("ErrorLabel", signUpDialog);
    
        registerButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent evt) {
                if (checkEmailField(emailField.getText())) {
    
                } else {
                    errorLabel.setText("Please enter a valid email address");
                    errorLabel.getStyle().setFgColor(ColorUtil.CYAN);
                    signUpDialog.animate();
                }
                errorLabel.setText("Please enter a valid email address");
                errorLabel.getStyle().setFgColor(ColorUtil.CYAN);
                signUpDialog.revalidate();
            }
        });
    signUpDialog.show();
    }
    

    【讨论】:

      【解决方案2】:

      它应该工作 输出中是否有任何错误消息? 你能把这个注册对话框的全部代码贴出来吗?

      【讨论】:

        【解决方案3】:

        通过在日志中打印一些文本来检查该语句的 else 部分是否实际被调用。

        试试:

        errorLabel.getParent().revalidate();
        //OR
        errorLabel.getParent().repaint();
        

        【讨论】:

        • 我在 if 语句下面添加了代码仍然无法正常工作
        • 试试forceRevalidate();
        • 我明白了。我需要将 signUpDialog.showPacked() 放在方法的底部。
        猜你喜欢
        • 1970-01-01
        • 2019-11-26
        • 1970-01-01
        • 1970-01-01
        • 2012-10-10
        • 1970-01-01
        • 1970-01-01
        • 2017-03-14
        • 1970-01-01
        相关资源
        最近更新 更多