【问题标题】:Searching an arraylist for username and password在数组列表中搜索用户名和密码
【发布时间】:2014-05-30 12:11:01
【问题描述】:

我正在为一个程序创建一个登录窗口。我无法让我的程序搜索我的数组列表并返回用户名,然后再次搜索密码。

    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) {
        }
    }
}

该代码应该在按下“登录”按钮时运行程序,但它目前不工作。

【问题讨论】:

  • 你的main() 方法在哪里或者你是如何调用这个类的?

标签: java swing arraylist awt


【解决方案1】:

下面一行有问题:

if (list.get(i).password.equals(passwordField.getPassword())){

JPasswordField#getPassword() return char[] 当您将字符串 passwordchar[] 进行比较时,这是错误的。

尝试Arrays.equals() 比较数组如下:

Arrays.equals(list.get(i).password.toCharArray(), passwordField.getPassword());

我建议您使用Map 代替ArrayList。将username设为Map的key。

使用

Map<String,Login> map = new HashMap<String,Login>();

而不是

ArrayList<Login> list= new ArrayList<Login>();

Map 的搜索速度比 ArrayList 更快。

【讨论】:

  • 我绝对同意这一点。映射是一种更好的方法,因为您可以将特定密码映射到特定用户名。
  • 但我不是在比较两个数组,而是将单个字符串与数组列表中对象的字符串类型的成员变量进行比较。将查看地图
  • passwordField.getPassword() 返回什么?
  • 谢谢。我相信我现在明白了。我已经按照您的建议进行了更改,但仍然无法正常工作。我在找到帐户后尝试插入打印方法,但没有响应。这表明 'if (list.get(i).account.contains(textField.getText()))' 也存在问题。有什么建议吗?
  • 编译并运行得很好。当我按下按钮时,什么也没有发生
【解决方案2】:

希望我的笔记能有所帮助:

  1. 没有使类可运行的main 方法
  2. list.add(test); 的位置不允许在那里。它应该在构造函数/方法中。

【讨论】:

  • 感谢您的更正。我已经相应地编辑了帖子。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-07
  • 2015-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多