我正在编写一个简单的用户登录屏幕,它将通过序列化存储用户。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.");
}
}
}