我正在使用 Java Swing 开发我的第一个 GUI 程序。我有一个登录表单类,它读取用户名和密码,然后检查本地数据库中是否存在匹配的用户名/密码组合。如果有,我希望能够将用户名从 JFrame 对象返回到我的主类。这只会在我点击我的登录按钮的动作触发后发生。
我不知道如何让这个类在特定时间之后返回:
public class Login extends JFrame {
//declaration of components
private JTextField userText = new JTextField(12);
private JPasswordField passText = new JPasswordField(12);
//declaration of variables
String username;
String password;
//...
class loginAction extends AbstractAction{
loginAction(){
super("Login");
putValue(SHORT_DESCRIPTION, "Click to login");
}
public void actionPerformed(ActionEvent e){
userLogin();
}
}
public String userLogin(){
String sql = "SELECT * from Employees " +
"WHERE Username = ? AND Password = ?";
try(PreparedStatement ps = con.prepareStatement(sql)){
ps.setString(1, userText.getText());
ps.setString(2, new String(passText.getPassword()));
ResultSet rs = ps.executeQuery();
if(rs.next())
//RETURN USERNAME BACK TO MAIN CLASS...How?
else
JOptionPane.showMessageDialog(this, "Incorrect username or password.", "", JOptionPane.ERROR_MESSAGE);
}
catch (SQLException e) {
utils.Print.print(e.getMessage());
printSuppressed(e);
}
finally{
}
}
//...
Login(Connection connect){
///...
JButton login = new JButton(new loginAction())
//...
}