2016-02-25 28 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