中指定的新值,因此我创建了一个名为savedName的变量,然后将其值设置为动作侦听器方法中的新值。它将更改的值打印在动作侦听器中,但仅在动作侦听器中打印。似乎一旦动作侦听器发生,变量的值就会变回“null”(默认值)。 username_txt是用户输入信息的文本字段。但正如我所说的那样,只是在动作侦听器方法中临时更改该值,并且一旦动作侦听器发生,它就会变回默认值。我用粗体显示了savedName的语句。变量的值不会更改为在actionListener
public class LoginWindow extends javax.swing.JFrame {
/**
* Creates new form LoginWindow
*/
Connection conn = null;
ResultSet rs = null;
PreparedStatement pst = null;
**private String savedName;**
public LoginWindow() {
initComponents();
conn = JavaConnect.ConnecrDb();
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String user_sql = "select * from Users where Username=? and Password=? ";
try{
pst = conn.prepareStatement(user_sql);
pst.setString(1, username_txt.getText());
savedName = username_txt.getText();
System.out.println(savedName);
pst.setString(2, password_txt.getText());
rs = pst.executeQuery();
if(rs.next()){
JOptionPane.showMessageDialog(null, "Wecome!");
rs.close();
pst.close();
this.dispose();
UserMainWindow wind = new UserMainWindow();
wind.setVisible(true);
}
else{
JOptionPane.showMessageDialog(null, "Username and/or Password is not correct");
}
}
catch(Exception e){
JOptionPane.showMessageDialog(null,e);
}
finally {
try{
rs.close();
pst.close();
}
catch(Exception e){
}
}
}
您的操作侦听器是一种私有方法。那应该是? – 2014-12-02 06:25:09
你确定你的'jButton2ActionPerformed'方法已被调用吗? – 2014-12-02 06:25:38
该动作侦听器是一个登录按钮。我在actionListener中做了System.out.println(savedName),它打印出正确的值。我需要另一个类的savedName变量,并且每当我在另一个类中使用savedName时,它总是使用savedName的默认值。该值在actionPerformed方法中暂时更改,但从不保存。 – user3754524 2014-12-02 06:28:46