这是我的桌面应用程序登录页面的代码。 在这个动作列表器中的if条件不能正常工作。 它总是显示错误的密码
final String n1 = textField.getText();
lblJph.setFont(new Font("Times New Roman", Font.BOLD | Font.ITALIC, 34));
JLabel lblNewLabel = new JLabel("User Name");
lblNewLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
passwordField = new JPasswordField();
final String pass = passwordField.getText();
//final char[] n3 = passwordField.getPassword();
//final char[] n2 = {'a','d','m','i','n'};
//final boolean check = n3.equals(n2);
JLabel lblNewLabel_1 = new JLabel("Password");
JLabel label = new JLabel("");
JButton btnNewButton = new JButton("Submit");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(n1.equals("admin") && pass.equals("sales")){
SALES j=new SALES();
j.setVisible(true);
dispose();
} else {
JOptionPane.showMessageDialog(null,"Wrong Password / Username");
textField.setText("");
passwordField.setText("");
textField.requestFocus();
}
}
});
答案 0 :(得分:0)
在actionListener中调用{/ 1}} 。 pass永远不会获得更新的值
同样适用于n1
答案 1 :(得分:0)
我认为您需要阅读actionlistener
中的密码字段和用户名字段btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String n1 = textField.getText();
String pass = passwordField.getText();
if(n1.equals("admin") && pass.equals("sales")){
SALES j=new SALES();
j.setVisible(true);
dispose();
}
else {
JOptionPane.showMessageDialog(null,"Wrong Password / Username");
textField.setText("");
passwordField.setText("");
textField.requestFocus();
}
}
});
答案 2 :(得分:0)
尝试以下方式从密码字段 INSIDE THE ACTION LISTENER 中检索密码:
char pas[]=passwd.getPassword();
String pass=new String(pas);
然后检查
pass.equals("sales");