java - 如何通过单击按钮来销毁框架?
问题描述
我想通过单击一个按钮来破坏框架。我到处搜索,似乎我做对了。但它不工作。
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();
}
}
解决方案
username_field和password_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。
推荐阅读
- vba - 从 ms access 2016 中的表单上的未绑定文本框中更新记录字段
- python - 如何使用 Skyfield 或 PyEphem 来确定过去五连词的日期?
- python - Sympy - 当表达式包含许多符号时,集成很慢
- javascript - 使用 dc.js 从存储在字典中的已处理数据创建条形图
- c# - 使用 ASP.NET Core 2.2 的页面名称
- python - Compute Engine n1-standard 仅使用 50% 的 CPU
- node.js - Heroku - 获取请求的请求超时
- autohotkey - AHK:有人可以让我的脚本按住鼠标左键拖动鼠标吗?
- c++ - 在c++中定义一组set的迭代器
- angular - 无法在 Angular 中创建单例服务(按照文档)