2016-02-25 49 views
1

我试图通过单击该按钮来更改标签的文本。所以我使用setText进行更改,然后调用revalidate(),但是当我运行它并按下按钮时,文本在标签中不会更改。这是我的代码。 我在做什么错?Codename one。使用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语句下添加了代码,以防其他情况未被调用。仍然没有工作...

回答

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(); 
} 
1

它应该工作 输出中是否有任何错误信息? 你能发布这个signupDialog的整个代码吗?

+0

输出中没有错误消息。 – Kyri33

1

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

尝试:

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

我添加了if语句下面的代码仍然不能正常工作 – Kyri33

+0

尝试'forceRevalidate();' – Diamond

+1

我明白了。我需要在方法底部创建signUpDialog.showPacked()。 – Kyri33