首页 > 解决方案 > 如何通过单击按钮来销毁框架?

问题描述

我想通过单击一个按钮来破坏框架。我到处搜索,似乎我做对了。但它不工作。

public class LoginWindow {

    public void CreateLoginWindow () {

        /** Set Style to Main Frame **/
        JFrame main_window = new JFrame();
        main_window.setUndecorated(true);
        main_window.setLocationRelativeTo(null);
        main_window.setLayout(new BorderLayout());
        main_window.setLayout(new FlowLayout());
        main_window.setVisible(false);
        main_window.setContentPane(new JLabel(new ImageIcon("images/MainWindow-bg.jpg")));
        main_window.setExtendedState(JFrame.MAXIMIZED_BOTH);
        main_window.setSize(1920,1080);
        main_window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        /** Some Codes **/

        JButton login_button = new JButton("Click to Exit");
        login_button.setBounds(920,480,120,45);

        /** Login Button Action **/
        login_button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            ValidateLogin validateLogin = new ValidateLogin();
            Boolean valid = validateLogin.ValidateLoginAction(username_field.getText(),password_field.getText());
            main_window.dispose();
            }
        });

        main_window.add(login_button);
        main_window.setVisible(true);

    }

}

似乎ValidateLogin validateLogin = new ValidateLogin(); Boolean valid = validateLogin.ValidateLoginAction(username_field.getText(),password_field.getText());会造成一些问题。

这是我的ValidateLogin课:

public class ValidateLogin {

    public Boolean ValidateLoginAction (String username, String password){

        ConnectToDB validate_login = new ConnectToDB();
        String right_password = validate_login.GetPassOfAnUsername(username);
        if ( right_password.equals(password) ){
            return true;
        } else {
            return false;
        }

    }

}

这是我的ConnectToDB课:

public class ConnectToDB {

    /** Connect to Database **/
    private Connection connect() {

        String url = "jdbc:sqlite:E://Resturant Project/db/Resturant.db";
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(url);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
        return connection;
    }

    /** Get Password of an Username **/
    public String GetPassOfAnUsername(String username){

        String password = "SELECT Password FROM Person WHERE UserName = '" + username +"'";

        try (Connection connection = this.connect();
             PreparedStatement statement= connection.prepareStatement(password);
             ResultSet results = statement.executeQuery()) {

            return results.getString("Password");

        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }

        return null;
    }

}

这是我的MainWindow课:

public class MainWindow {

    public static void main(String[] args) {
        LoginWindow loginWindow = new LoginWindow();
        loginWindow.CreateLoginWindow();
    }

}

标签: javaswingjframe

解决方案


username_fieldpassword_field变量是否已在某处实例化?当调用actionPerformed方法时,您访问 getText() 方法的行可能会引发 NullPointerException ,因此程序永远不会到达 main_window.dispose() 行。
在尝试从它们访问 getText() 方法之前,尝试在执行 actionPerformed 方法时检查这两个变量是否为空。
另外,请检查数据库连接是否已成功建立。

ConnectToDB validate_login = new ConnectToDB();
String right_password = validate_login.GetPassOfAnUsername(username);

如果validate_login为 null ,第二行也可能引发 NullPointerException,因为如果连接失败,您的代码将从 ConnectToDB() 构造函数返回 null。


推荐阅读