我正在编写一个简单的用户登录屏幕,它将通过序列化存储用户. GUI很好,剩下的就是实现序列化,但是当用户输入错误的密码或用户名时,我无法正确显示warningLabel文本.它将显示第一条错误消息,但如果发生不同的错误,则标签保持不变.我需要标签改变每次都有错误.我将在下面发布完整的代码.
UserCreateAccountGUI类:
package userInfoAndSerialization;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
public class UserCreateAccount implements ActionListener {
public static int numOfUsers;
String username;
String password;
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
UserCreateAccount ucaGUI = new UserCreateAccount();
ucaGUI.start();
}
JFrame frame;
JPanel panel;
JTextField usernameField;
JPasswordField passwordField;
JPasswordField confirmPasswordField;
JLabel warningLabel;
public void start() {
frame = new JFrame("Create a new account");
panel = new JPanel();
panel.setLayout(new GridBagLayout());
frame.getContentPane().add(BorderLayout.CENTER, panel);
panel.setBackground(Color.ORANGE);
JLabel userLabel = new JLabel("Username:");
JLabel passwordLabel = new JLabel("Password:");
JLabel confirmPasswordLabel = new JLabel("Confirm Password:");
usernameField = new JTextField(15);
passwordField = new JPasswordField(15);
confirmPasswordField = new JPasswordField(15);
GridBagConstraints right = new GridBagConstraints();
right.anchor = GridBagConstraints.WEST;
GridBagConstraints left = new GridBagConstraints();
left.anchor = GridBagConstraints.EAST;
right.weightx = (int) 2;
right.fill = GridBagConstraints.REMAINDER;
right.gridwidth = GridBagConstraints.REMAINDER;
// actual GUI
panel.add(userLabel, left);
panel.add(usernameField, right);
panel.add(passwordLabel, left);
panel.add(passwordField, right);
panel.add(confirmPasswordLabel, left);
panel.add(confirmPasswordField, right);
frame.setSize(300, 250);
frame.setVisible(true);
JButton createAccount = new JButton("Create this account");
frame.getContentPane().add(BorderLayout.SOUTH, createAccount);
createAccount.addActionListener(this);
warningLabel = new JLabel();
frame.getContentPane().add(BorderLayout.NORTH, warningLabel);
}
// this is where the problem is.
public void actionPerformed(ActionEvent event) {
if (!(passwordField.getPassword().toString().equals(confirmPasswordField.getPassword().toString()))) {
warningLabel.setText("Your passwords do not match! Please try again!");
} else if (passwordField.getPassword().toString().length() < 1 ) {
warningLabel.setText("Your password is not long enough! Please try again!");
} else if (usernameField.getText().length() < 1) {
warningLabel.setText("Your username is not long enough! Please try again!");
} else {
warningLabel.setText("Account created successfully.");
}
}
}
最佳答案 这不会飞:
passwordField.getPassword().toString().
equals(confirmPasswordField.getPassword().toString())
你最好打印出在char数组上调用.toString()的结果,看看我究竟是什么意思.
例如,当我跑:
String fooString = "Foo";
char[] fooArray = fooString.toCharArray();
System.out.println(fooArray.toString());
它不会像你期望的那样返回“Foo”,而是返回char数组的典型和期望的toString()表示:[C @ 19821f.请注意,如果您运行此操作,您的哈希码编号将与我的不同(如果我再次运行它,则相同!).
最好使用Arrays类equals(…)方法让你比较两个char数组.即,
char[] pw1 = passwordField.getPassword();
char[] pw2 = confirmPasswordField.getPassword();
if (Arrays.equals(pw1, pw2)) {
//...
}
注意:一个糟糕的解决方案是使用新的String(myCharArray)将char数组转换为“真正的”字符串,但我强烈建议不要这样做,因为它会使你的密码非常弱并容易破解.