任何人都可以帮助我修复错误“无法将用户分配给变量”[关闭]

问题描述 投票:0回答:0

我正在尝试使用“用户”构造函数将用户名和密码存储在名为“用户”的 ArrayList 中,然后在 for 循环中搜索每个元素以查找登录系统,但出现错误“用户无法分配给多变的”。我需要下面显示的代码做的就是搜索“用户”列表,并将用户名和密码与在 TextField 和 PasswordField 中输入的用户名和密码进行比较。

用户类别:

package System;
import java.util.ArrayList;

public class User {
    private String username;
    private String password;
    
    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }
    
    public String getUsername() {
        return username;
    }
    
    public String getPassword() {
        return password;
    }

    public static void main(String[] args) {
        ArrayList<User> users = new ArrayList<>();
        users.add(new User("Daniel", "password0"));
        users.add(new User("Oisin", "password1"));
        users.add(new User("Paul", "password2"));
        users.add(new User("Finn", "password3"));
        

    }

}

for循环错误:for(User user: users){

package System;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.*;

public class LoginGUI {
    private static JFrame frame;
    private static JPanel panel;
    private static JLabel userLabel;
    private static JLabel passwordLabel;
    private static JTextField userText;
    private static JPasswordField passwordText;
    private static JButton button;
    
    public LoginGUI() {
        frame = new JFrame("Login GUI");
        panel = new JPanel();
        frame.setSize(350, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel);
        panel.setLayout(null);
        
        userLabel = new JLabel("Username");
        userLabel.setBounds(10, 20, 80, 25);
        panel.add(userLabel);
        userText = new JTextField();
        userText.setBounds(100, 20, 165, 25);
        panel.add(userText);
        
        passwordLabel = new JLabel("Password");
        passwordLabel.setBounds(10, 50, 80, 25);
        panel.add(passwordLabel);
        passwordText = new JPasswordField();
        passwordText.setBounds(100, 50, 165, 25);
        panel.add(passwordText);
        
        button = new JButton("Login");
        button.setBounds(10, 80, 80, 25);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = userText.getText();
                String passw = passwordText.getText();
                for (User user: users) {
                    if(user.getUsername().equals(username) && user.getPassword().equals(passw)) {
                        frame.dispose();
                        new MainMenuGUI();
                    }else {
                        JOptionPane.showMessageDialog(null, "User not found please try again");
                    }
                }
            }
        });
        panel.add(button);
        
        button = new JButton("Create");
        button.setBounds(100, 80, 100, 25);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
                new NewUserGUI();
            }
        });
        panel.add(button);
    }

    public static void main(String[] args) {
        new LoginGUI();
        frame.setVisible(true);
    }

}
java
© www.soinside.com 2019 - 2024. All rights reserved.