【问题标题】:Close window after checking检查后关闭窗口
【发布时间】:2021-08-10 23:43:43
【问题描述】:

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

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); */
    }
});

【问题讨论】:

  • 也许试试InputVerifier,检查here
  • @trashgod 你能否检查一个基于this comment 的问题,其中提到框架图标在 Mac 上不起作用? AFAIR 您提供的屏幕截图表明 Mac 不使用自定义框架图标。 (不过,我可能记错了细节。)
  • OP: 1) 为了尽快获得更好的帮助,edit 添加minimal reproducible exampleShort, Self Contained, Correct Example。 2) 请学习常见的 Java 命名法(命名约定 - 例如EachWordUpperCaseClassfirstWordLowerCaseMethod()firstWordLowerCaseAttribute,除非它是 UPPER_CASE_CONSTANT)并始终如一地使用它。请特别注意,_ 字符是通用命名法一部分的唯一情况是常量。

标签: java swing


【解决方案1】:

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

另外,请阅读有关使用多个 JFrame 的内容。 The Use of Multiple JFrames: Good or Bad Practice?

现在是代码部分...

完成用户验证后,您需要创建主窗口的实例并使其可见。之后,您可以关闭第一个 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();
       }
    }
});

【讨论】:

    猜你喜欢
    • 2011-09-10
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    • 1970-01-01
    • 2017-09-14
    • 1970-01-01
    • 2012-09-02
    相关资源
    最近更新 更多