如何中途停止Button ActionListener等待用户再次点击?

How to stop Button ActionListener midway and wait for the user to click it again?

我真的很需要这个问题的解决方案,但经过 2 小时的搜索仍未找到解决方案。我正在通过开发简单的应用程序来学习 Java AWT。

我创建了一个注册 page/window/frame,它通过各自的文本字段获取用户名、密码和确认密码,并在单击 "Sign Up" 按钮时将它们添加到数据库中,如果并且仅当两个密码匹配时。如果不匹配,密码文本字段将被清除,用户需要重新输入值并再次单击 "sign Up" 按钮。这需要循环进行。我已将所有必需代码行(包括密码不相等逻辑)放入 "Sign Up" 按钮的 ActionListener 中。

我正在发布代码中按钮的 ActionListener 部分。您会注意到明显的逻辑错误,例如,在使用 setText("") 清除密码的 TextFields 后,密码实际上匹配,因为两者都是空字符串。但是,即使我只清除两个 TextFields 中的一个,一旦执行了 ActionListener,我就无法在 TextFields 中重新输入新值,应用程序将永远挂起,直到强制关闭。

signupButton.addActionListener(new ActionListener()
{
    public void actionPerformed(ActionEvent e)
    {               
        String username = "";
        String password = "";
        String confirmPass = "";

        do
        {
            username = usernameTF.getText();
            password = passwordTF.getText();
            confirmPass = confirmPassTF.getText();

            Label passErrorMsg = new Label("Passwords do not match. Enter again.");

            if(password.equals(confirmPass))
            {
                passErrorMsg.setVisible(false);
                break;
            }

            passErrorMsg.setBounds(70, 320, 205, 20);
            signupWindow.add(passErrorMsg);
            passErrorMsg.setVisible(true);

            passwordTF.setText(""); //If I comment this statement, the app hangs.
            //Else in the next iteration, the loop breaks since both the strings become empty

            confirmPassTF.setText("");

        }while(true);

       //Some more lines of code to work with the database
    }
});

您的 while 循环不属于此范围,因为虽然它适用于线性控制台程序,但会阻塞事件驱动程序中的 Swing 事件线程或任何其他事件线程,从而使程序冻结且无用。相反,如果输入错误,您可能只想清除对话框的文本字段,并在 JOptionPane 中显示错误消息。事实上,您可能只需要 if / else 块而不需要 while 循环:

SignupButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {               
        String Username = "";
        String Password = "";
        String ConfirmPass = "";

        Username = UsernameTF.getText();
        Password = PasswordTF.getText();
        ConfirmPass = ConfirmPassTF.getText();

        if(Password.equals(ConfirmPass)) {
            PassErrorMsg.setVisible(false);

            // do database stuff here

        } else {
            // here clear fields and show an error message
            // consider incrementing an error count as well
        }
    }
});

同样关键的问题是,在创建事件驱动程序时,您必须以非线性事件驱动的方式思考。线性控制台代码逻辑将不起作用(在这些情况下)。

其他问题:您不应该使用字符串来保存密码,因为这些密码将被保存到字符串池中,很容易被黑客入侵。此外,是的,正如 Camickr 所说,学习并遵循 Java 命名约定,包括以小写字母开头的变量和方法名称以及以大写字母开头的 class 名称。避免使用 setBounds(...)null 布局,因为它们会导致 GUI 无法在所有平台上正常工作。而是学习和使用布局管理器。

如果 functions/methods 只应在单击按钮时触发一次,则不应执行迭代,上面的代码可能会陷入无限循环。