我正在为程序创建一个登录窗口。我无法让我的程序搜索我的arraylist并返回用户名然后再次搜索密码。
public class Login extends JFrame {
public String firstName, lastName, account, birthYear, password;
Login(String fn, String ln, String acc, String by, String pw){
firstName = fn;
lastName = ln;
account = acc;
birthYear = by;
password = pw;
}
Login current;
ArrayList<Login> list= new ArrayList<Login>();
int count;
private JPanel contentPane;
private JPasswordField passwordField;
private final Action action = new SwingAction();
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Login frame = new Login();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Login() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label = new JLabel("");
label.setBounds(217, 5, 212, 126);
contentPane.add(label);
JButton btnLogin = new JButton("Login");
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
for (int i = 0; i<list.size(); i++){
if (Arrays.equals(list.get(i).password.toCharArray(), textField.getText().toCharArray())){
System.out.println("found the account");
if (Arrays.equals(list.get(i).password.toCharArray(), passwordField.getPassword())){
current = list.get(i);
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
overview frame = new overview();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
else{
JOptionPane.showMessageDialog(null, "Password does not match our records", "Error", JOptionPane.INFORMATION_MESSAGE);
}
}
else{
JOptionPane.showMessageDialog(null, "Account number not found", "Error", JOptionPane.INFORMATION_MESSAGE);
}
}
}
});
btnLogin.setAction(action);
btnLogin.setBounds(108, 186, 89, 23);
contentPane.add(btnLogin);
JButton btnRegister = new JButton("Register");
btnRegister.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Register frame = new Register();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
});
btnRegister.setBounds(217, 186, 89, 23);
contentPane.add(btnRegister);
passwordField = new JPasswordField();
passwordField.setBounds(172, 148, 142, 20);
contentPane.add(passwordField);
JPanel panel = new JPanel();
panel.setBackground(Color.BLUE);
panel.setBounds(39, 32, 335, 51);
contentPane.add(panel);
JLabel lblLogin = new JLabel("Login");
lblLogin.setFont(new Font("Verdana", Font.BOLD, 15));
panel.add(lblLogin);
lblLogin.setForeground(SystemColor.window);
lblLogin.setBackground(Color.BLUE);
JLabel lblAccountNumber = new JLabel("Account Number:");
lblAccountNumber.setBounds(49, 101, 113, 14);
contentPane.add(lblAccountNumber);
JLabel lblPassword = new JLabel("Password:\r\n");
lblPassword.setBounds(49, 151, 113, 14);
contentPane.add(lblPassword);
textField = new JTextField();
textField.setBounds(172, 98, 142, 20);
contentPane.add(textField);
textField.setColumns(10);
}
public void printLogin(Login l) {
System.out.println(l.firstName);
System.out.println(l.lastName);
System.out.println(l.account);
System.out.println(l.birthYear);
System.out.println(l.password);
}
private class SwingAction extends AbstractAction {
public SwingAction() {
putValue(NAME, "Login");
putValue(SHORT_DESCRIPTION, "Login to your account");
}
public void actionPerformed(ActionEvent e) {
}
}
}
当按下“登录”按钮但当前无效时,代码应该运行程序。
答案 0 :(得分:2)
下面有一个问题:
if (list.get(i).password.equals(passwordField.getPassword())){
JPasswordField#getPassword()返回char[]
,比较字符串password
和char[]
错误。
尝试Arrays.equals()比较数组如下:
Arrays.equals(list.get(i).password.toCharArray(), passwordField.getPassword());
我建议您使用Map
代替ArrayList
。将username
作为地图的关键。
使用
Map<String,Login> map = new HashMap<String,Login>();
而不是
ArrayList<Login> list= new ArrayList<Login>();
Map
搜索速度比ArrayList
更快。
答案 1 :(得分:0)
我希望我的笔记能够提供帮助:
main
方法来使类可运行list.add(test);
的位置。它应该在构造函数/方法中。