4

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

1 回答 1

2

这不会飞:

passwordField.getPassword().toString().
       equals(confirmPasswordField.getPassword().toString())

您最好打印出调用.toString()char 数组的结果,以确切了解我的意思。

例如,当我运行时:

String fooString = "Foo";
char[] fooArray = fooString.toCharArray();
System.out.println(fooArray.toString());

它不会像您期望的那样返回“Foo”,而是返回toString()char 数组的典型和预期表示:[C@19821f. 请注意,如果您运行此程序,您的哈希码编号将与我的不同(如果我再次运行此程序,则相同!)。

最好使用 Arrays 类equals(...)方法让您比较两个 char 数组。IE,

char[] pw1 = passwordField.getPassword();
char[] pw2 = confirmPasswordField.getPassword();
if (Arrays.equals(pw1, pw2)) {
   //...
}

注意:一个不好的解决方案是使用 将 char 数组转换为“真正的”字符串new String(myCharArray),但我强烈建议不要这样做,因为它会使您的密码非常脆弱且容易破解。

于 2013-04-22T22:55:21.677 回答