gpt4 book ai didi

java - 为什么我的登录用户 GUI 不能正确设置警告标签?

转载 作者:搜寻专家 更新时间:2023-11-01 03:25:05 25 4
gpt4 key购买 nike

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

您最好打印出在字符数组上调用 .toString() 的结果,以便准确理解我的意思。

例如,当我运行时:

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

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

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

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

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

关于java - 为什么我的登录用户 GUI 不能正确设置警告标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16158047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com