检查后关闭window

Close window after checking

我遇到了一个小问题,我无法解决。 我想让我的代码检查电子邮件和密码是否匹配,然后 然后 关闭 window。这个动作:

btnLogin.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {

        boolean status = Email_Verification.email_validation(email_text.getText().toString().trim());
        if (status) {
            lbl_inco_email.setText(null);
        } else {
            lbl_inco_email.setText("Please enter a valid email");
        }

        boolean stat = Password_Verification.password_validation(password_Field.getPassword().toString());
        if (stat) {
            lbl_inco_pwd.setText(null);
        }   else {
            lbl_inco_pwd.setText("Please enter a valid Password");
        }

        /*      Exit and redirect to the main application if the email/pwd matches the database */

        /** MAIN__WINDOW __EXIT__ONCLICK__ = new MAIN__WINDOW();
        __EXIT__ONCLICK__.setVisible(true); */
    }
});

我假设您在打开新框架和处理登录框架时遇到问题,因为如果您在用户验证方面遇到问题,我们没有足够的信息来实际提供帮助。

另外,请阅读这篇关于使用多个 JFrame 的文章。 The Use of Multiple JFrames: Good or Bad Practice?

现在是代码部分...

完成用户验证后,您需要创建主实例 window 并使其可见。之后您可以关闭第一个 JFrame。

您的 actionPerformed 看起来像这样:

btnLogin.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
       // Let's assume you have a boolean variable that handles user validation 
          //for simplicity...
       if(userVerified) {
         // In this example let's call the main application window MainFrame
          MainFrame mF = new MainFrame();
          // Set it to visible
          mF.setVisible(true);
          mF.setLocationRelativeTo(null);
          // Now you can close the first JFrame
          this.dispose();
       }
    }
});