代号一个。使用revalidate()动态更改标签文本无效

时间:2016-02-25 17:05:19

标签: codenameone

我正试图点击按钮更改标签的文字。所以我使用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语句下添加了代码,以防万一其他人没有被调用。仍然没有工作......

3 个答案:

答案 0 :(得分:1)

它应该有用 输出中是否有任何错误消息? 你能发布这个signupDialog的全部代码吗?

答案 1 :(得分:1)

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

尝试:

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

答案 2 :(得分:1)

替换此代码signUpDialog.showPacked(BorderLayout.CENTER,true); with signupDilaog.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();
}