[英]Java Swing Dialog issue
當按下“注冊”按鈕時,會彈出一個對話框,要求用戶輸入密碼(設置為“ qwerty”)。 我希望它繼續顯示對話框,直到密碼正確為止。 方法如下:
private void ItemInregistrareActionPerformed(java.awt.event.ActionEvent evt) {
JOptionPane dialog = new JOptionPane();
dialog.setWantsInput(true);
dialog.showInputDialog("Password please:");
while(dialog.getInputValue()!="qwerty")
dialog.showInputDialog("Mai baga o fisa.");
ItemInregistrare.setEnabled(false);
ItemOpen.setEnabled(true);
ItemSave.setEnabled(true);
}
問題是,即使密碼正確,它也永遠不會消失。 有小費嗎?
JOptionPane.showInputDialog
是靜態方法,不需要JOptionPane
任何實例。 此外,它已經返回輸入的值;如果用戶按“取消”,則返回null
。 因此,您無需調用dialog.getInputValue()
。
您可以嘗試這樣的事情:
String pwd;
do {
pwd = JOptionPane.showInputDialog("Password please:");
} while (pwd != null && !pwd.equals("qwerty"));
if (pwd == null) {
JOptionPane.showMessageDialog(null, "You pressed cancel");
} else {
JOptionPane.showMessageDialog(null, "Password is correct");
}
嘗試使用!dialog.getInputValue().equals("qwerty")
比較字符串
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.