如何在GUI类中访问Client类的HashMap实现登录功能?
登录表单验证实现问题
我正在为应用实现一个简单的登录表单,已创建Client.java类并在其中实例化了一个HashMap:
package com.xbank.event; import com.xbank.service.*; import java.text.*; import java.util.*; public class Client { public static Map<String, Client> clientMap; public String name; public String username; public String password; public Double balance; public String address; public String phone; // Default Constructor public Client() { name = ""; address = ""; phone = ""; username = ""; password = ""; balance = 0.0; clientMap = new HashMap<String, Client>(); } // Parameterized Constructor public Client(String name, String username,String password, String phone, String address, Date date) { this.name = name; this.username = username; this.password = password; this.address = address; this.phone = phone; } public void verify_login(String username, String password) { Client client = new Client(); client = new Client(username, password, name, address, phone, new Date()); clientMap.put(username, client); if (clientMap.containsKey(username)) { System.out.println(username); client = client.clientMap.get(username); if(client.password.equals(password)) { System.out.println(password); } else { System.out.println("Wrong password"); } } else { System.out.println("Wrong username"); } } public static Map<String, Client> getLoginInfo() { return clientMap; } void showClientInfo() { System.out.println("Name: "+name); System.out.println("Phone Name: "+phone); System.out.println("Address Name: "+address); } public String getName() { return name; } public String getPhone() { return phone; } public String getAddress() { return address; } public String getUsername() { return username; } public String getPassword() { return password; } // end of class }
我想要在已创建的GUI类中访问该HashMap,并设置登录验证条件,但不知道如何实现。
GUI类代码:
public class GUI { // remake the screen public void remakeScreen(JFrame frame, JPanel f) { f.setLayout(null); frame.setContentPane(f); frame.setVisible(true); } //------------------------------------------------------------ // Login Related Screen Functions | //------------------------------------------------------------ // sign in form which is displayed at the start of program public void openSignInForm(JFrame frame, Client client) { JPanel f = new JPanel(); f.setBackground(Color.black); // main screen picture JLabel imgLabel = new JLabel(new ImageIcon( System.getProperty("user.dir") + "/src/main/java/com/xbank/icon/image-1.png")); imgLabel.setBounds(300,50,450, 400 ); f.add(imgLabel); //---------------------------------Creating Labels------------------------------------------ JLabel l_hSignIn = new JLabel("Sign In"); l_hSignIn.setFont( l_hSignIn.getFont().deriveFont(30f) ); l_hSignIn.setBounds(120,24,100, 40); l_hSignIn.setForeground(Color.white); f.add(l_hSignIn); JLabel l_UserName = new JLabel("User Name"); l_UserName.setBounds(140,120,100, 40); l_UserName.setForeground(Color.white); f.add(l_UserName); JLabel l_Password = new JLabel("Password"); l_Password.setBounds(140,200,100, 40); l_Password.setForeground(Color.white); f.add(l_Password); JLabel l_Signup = new JLabel("Dont have an account?"); l_Signup.setBounds(20,380,150, 40); l_Signup.setForeground(Color.white); f.add(l_Signup); //-----------------------------------Creating TextFields-------------------------------------------------- JTextField tf_UserName = new JTextField(); tf_UserName.setBounds(80,170,180, 25); f.add(tf_UserName); JPasswordField tf_Password = new JPasswordField(); tf_Password.setBounds(80,250,180, 25); f.add(tf_Password); //-------------------------------------Creating Buttons-------------------------------------------------- JButton btn_SignIn = new JButton("Sign In"); btn_SignIn.setFont( btn_SignIn.getFont().deriveFont(18f) ); btn_SignIn.setBackground( new Color(0, 204, 153) ); btn_SignIn.setForeground(Color.black); btn_SignIn.setBounds(120,300,110, 40); f.add(btn_SignIn); JButton btn_SignUp = new JButton("Sign Up"); btn_SignUp.setFont( btn_SignUp.getFont().deriveFont(18f) ); btn_SignUp.setBackground( new Color(0, 204, 153) ); btn_SignUp.setForeground(Color.black); btn_SignUp.setBounds(180,380,110, 40); f.add(btn_SignUp); //---------------------------------------SignUp functionality---------------------------------------------- btn_SignUp.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { open_Signup_form_1(frame, client); } }); //----------------------------------Sign in Functionality----------------------------------------------- btn_SignIn.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { Map<String, Client> user = (Map<String, Client>) new Client(); // HashMap<String, Client> logininfo = new HashMap<String, Client>(); user = user if(e.getSource() == btn_SignIn) { String userId = tf_UserName.getText(); String password = String.valueOf(tf_Password.getPassword()); if(client.containsKey(userId)) { if (client.get(userId).equals(password)) { Client Client = new Client( client.getName(), null, null, null, null, null ); frame.remove(f); frame.repaint(); frame.validate(); openClientMenu(frame,client); }else { System.out.println("Wrong username or password."); } } else { System.out.println("Wrong username or password."); } } } }); remakeScreen(frame, f); } }
问题修正与实现步骤
1. 修复Client类的核心问题
clientMap是静态变量,不能在构造方法中初始化,否则每次创建Client实例都会清空Map,改用静态代码块初始化,确保只执行一次。verify_login方法逻辑错误,原本每次调用都会新建用户并覆盖Map中的数据,改为静态验证方法,专门检查现有用户的账号密码。
修正后的Client类:
package com.xbank.event; import com.xbank.service.*; import java.util.*; public class Client { public static Map<String, Client> clientMap; public String name; public String username; public String password; public Double balance; public String address; public String phone; // 静态代码块初始化全局用户Map static { clientMap = new HashMap<>(); } // 默认构造方法 public Client() { this.name = ""; this.address = ""; this.phone = ""; this.username = ""; this.password = ""; this.balance = 0.0; } // 参数构造方法(注册时使用,自动将用户加入Map) public Client(String name, String username, String password, String phone, String address) { this.name = name; this.username = username; this.password = password; this.address = address; this.phone = phone; clientMap.put(username, this); } // 静态登录验证方法:返回账号密码是否匹配 public static boolean validateLogin(String username, String password) { if (clientMap.containsKey(username)) { Client client = clientMap.get(username); return client.password.equals(password); } return false; } public static Map<String, Client> getLoginInfo() { return clientMap; } void showClientInfo() { System.out.println("Name: " + name); System.out.println("Phone: " + phone); System.out.println("Address: " + address); } // Getter方法保持不变 public String getName() { return name; } public String getPhone() { return phone; } public String getAddress() { return address; } public String getUsername() { return username; } public String getPassword() { return password; } }
2. 修复GUI类的登录逻辑
- 不需要通过创建Client实例获取Map,直接调用
Client类的静态方法validateLogin()和getLoginInfo()即可。 - 原代码中
client.containsKey(userId)是错误调用,因为传入的client是单个用户对象,不是Map,改为直接操作静态clientMap。
修正后的GUI类登录按钮事件代码:
//----------------------------------Sign in Functionality----------------------------------------------- btn_SignIn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String userId = tf_UserName.getText().trim(); String password = String.valueOf(tf_Password.getPassword()).trim(); // 调用静态方法验证登录 boolean isLoginValid = Client.validateLogin(userId, password); if (isLoginValid) { // 获取登录成功的用户对象 Client loggedInClient = Client.getLoginInfo().get(userId); frame.remove(f); frame.repaint(); frame.validate(); openClientMenu(frame, loggedInClient); } else { System.out.println("Wrong username or password."); } } });
3. 注册逻辑补充(可选)
如果open_Signup_form_1是注册界面,注册成功后只需创建Client实例即可自动加入Map,示例代码:
// 注册界面提交按钮逻辑示例 public void open_Signup_form_1(JFrame frame, Client client) { JPanel currentPanel = new JPanel(); // ... 注册界面的输入框、标签等组件 ... JButton btn_SubmitSignUp = new JButton("Submit"); btn_SubmitSignUp.addActionListener(e -> { String name = tf_Name.getText().trim(); String username = tf_RegUsername.getText().trim(); String password = String.valueOf(tf_RegPassword.getPassword()).trim(); String phone = tf_Phone.getText().trim(); String address = tf_Address.getText().trim(); // 创建新用户,自动加入全局Map new Client(name, username, password, phone, address); // 返回登录界面 frame.remove(currentPanel); openSignInForm(frame, new Client()); }); currentPanel.add(btn_SubmitSignUp); remakeScreen(frame, currentPanel); }
内容的提问来源于stack exchange,提问作者sillyconAdmin
相关产品推荐
相关产品推荐

