4
我正在編寫一個簡單的用戶登錄屏幕,通過序列化來存儲用戶。 GUI很好,剩下的就是實現序列化,但是當用戶輸入不正確的密碼或用戶名時,我無法正確顯示文本warningLabel
。它將顯示第一條錯誤消息,但如果發生不同的錯誤,則標籤保持不變。我需要標籤來改變每次都有錯誤。我將在下面發佈整個代碼。爲什麼我的用戶GUI登錄不能正確設置警告標籤?
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.");
}
}
}
@Hovecraftfullofeels如果我需要以後保存這些密碼進行序列化,該怎麼辦?我需要能夠將它們變成用戶登錄可代表的內容?我應該怎麼做? – hasherr 2013-04-23 01:10:57
@HashSlingingHacker:你不應該保存序列化密碼。我不是安全專家,但這只是令我擔心。我猜想一種kludge會保存密碼的哈希碼,但即使這也讓我感到擔憂。 – 2013-04-23 01:11:45
@Hovecraftfullofeels我不保存密碼,我保存用戶帳戶,如在對象中。這些用戶帳戶將包含其他變量,如平衡和方法。如果我只是保存密碼/用戶名,我只是將它們保存到一個文本文件,但由於這些用戶對象具有與它們相關的其他東西,這些是將被序列化的東西。 – hasherr 2013-04-23 01:30:00