第一次触发按钮(动作)时,不会打印“用户名”。第二次按下该按钮时,将打印一次该值。在第三次单击时,该值被打印了两次。有人可以指出我的错误吗?
*这是我的第一个问题,请提供有关发布更好问题的提示:D
下面是主要方法:
public class DMS implements ActionListener{
private static String username;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
LoginFrame login = new LoginFrame();
login.setVisible(true);
}
public DMS(JTextField textField1) {
DMS.username = textField1.getText();
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(username);
}
}
这是动作监听器,它位于 jframe 中:
private void cmd_loginActionPerformed(java.awt.event.ActionEvent evt) {
ActionListener actionListener = new DMS(JTextField1);
someButton.addActionListener(actionListener);
}