繁体   English   中英

Java Swing对话框问题

[英]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.

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