如何通过单击按钮破坏框架?

时间:2019-06-01 15:49:08

标签: java swing jframe

我想通过单击一个按钮来破坏框架。我到处搜索,似乎我在做这件事。但它不起作用。

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();
    }

}

2 个答案:

答案 0 :(得分:0)

我看不到您的代码有任何问题。考虑到您还没有编写任何其他代码来干扰您提供的代码段的执行,因此只要按下 login_button

,该窗口应立即关闭

答案 1 :(得分:0)

我已经尝试过,并且我能够关闭窗口,并且jvm退出。我在下方提供了按钮操作代码段。

login_button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    ValidateLogin validateLogin = new ValidateLogin();
                    Boolean valid = validateLogin.ValidateLoginAction(username_field.getText(),password_field.getText());
                }
                catch(Exception ex) {
                    //handle exception
                }
                finally {
                    main_window.dispose();
                }
            }
        });