简体   繁体   中英

Java Swing Dialog issue

When pressed the "Inregistrare" button a dialog pops, requesting the user to enter a password (set to "qwerty"). I want it keep displaying dialogs until the password is correct. The method is the following:

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);

}

The problem is it never gets out of the while, even if the password is correct. Any tips?

JOptionPane.showInputDialog is a static method and does not need any instance of JOptionPane . Moreover, it already returns the entered value or null if user pressed Cancel. So you don't need to call dialog.getInputValue() .

You could try something like this:

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")比较字符串

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM