我正在使用Java Swing開發我的第一個GUI程序。我有一個登錄表單類,它讀入用戶名和密碼,然後檢查本地數據庫中是否有匹配的用戶名/密碼組合。如果有,我希望能夠將JFrame對象的用戶名返回到我的主類。這隻會發生在我點擊我的登錄按鈕的動作之後。發佈操作後從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())
//...
}
參見來接入信息使用多個JFrames,好/壞實踐?](http://stackoverflow.com/a/9554657/418556) –