我的登录屏幕中有一个密码字段。出于某种原因,当我输入正确的密码“u123”时,即使它是正确的,它也会给我不正确的密码错误消息。为什么要这样做。
我的代码如下:
btnLogin = new JButton("Login");
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
char[] userInput = passwordField.getPassword();
char[] correctPassword = { 'u', '1', '2', '3'};
if (userInput.equals(correctPassword)) {
JOptionPane.showMessageDialog(LoginScreen.this,
"Success! You typed the right password.");
} else {
JOptionPane.showMessageDialog(LoginScreen.this,
"Invalid password. Try again.",
"Error Message",
JOptionPane.ERROR_MESSAGE);
}
}
});
我知道这可能不是进行密码检查的最佳方法,但我只是一个初学者,只是想进行一些练习。