通过另一个类问题的数组列表迭代

发布于 2025-02-06 16:55:22 字数 2303 浏览 0 评论 0原文

这是我存储数据的类:

package entities;

import java.util.ArrayList;

public class EshopData {
    
    private static ArrayList<User> users = new ArrayList<User>();
    
    public ArrayList<User> getUsers() {
        return users;
    }

    public static void setUsers( ArrayList<User> users) {
        EshopData.users = users;
    }
}

这是我的主框架:

package ui;
import java.awt.event.*;
import java.util.ArrayList;

import javax.swing.*;

import entities.User;

public class Frame extends JFrame{
    
    public static void main(String[] args) {


        //FRAME
        JFrame frame = new JFrame("My first Eshop");

        frame.setSize(300, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        //frame.setVisible(true);
        frame.setLayout(null);
        

        //LABELS
        JLabel l1 = new JLabel("Username:");
        JLabel l2 = new JLabel("Password:");

        l1.setBounds(35, 60, 80, 20);
        l2.setBounds(35, 110, 80, 20);


        //TEXTFIELDS
        JTextField tf1 = new JTextField();
        JTextField tf2 = new JTextField();

        tf1.setBounds(150, 60, 90, 20);
        tf2.setBounds(150, 110, 90, 20);


        //BUTTONS
        JButton b1 = new JButton("Register");
        JButton b2 = new JButton("Login");

        b1.addActionListener(new ActionListener() {
            
            public void actionPerformed(ActionEvent e){

                RegisterFrame rframe = new RegisterFrame();
                rframe.setVisible(true);
                rframe.setSize(600, 600);
            }
        });


        b2.addActionListener(new ActionListener() {
            
            public void actionPerformed(ActionEvent e){
                
            }
        });

        b1.setBounds(35, 150, 100, 20);
        b2.setBounds(160, 150, 80, 20);

        //ADDING TO FRAME
        frame.add(l1);
        frame.add(l2);
        frame.add(tf1);
        frame.add(tf2);
        frame.add(b1);
        frame.add(b2);

        frame.setVisible(true);
    }
    
}

在我的主帧中,我有一个按钮“登录”(其'b2'按钮),我想创建一个ActionListener上面并搜索user.username,如果存在用户名,它将以后搜索user.password。 我不确定我是否正确解释了它,但是任何形式的帮助都将不胜感激。

如果您需要代码的其他任何部分,我在这里为您提供。提前!

This is the class where I store the data:

package entities;

import java.util.ArrayList;

public class EshopData {
    
    private static ArrayList<User> users = new ArrayList<User>();
    
    public ArrayList<User> getUsers() {
        return users;
    }

    public static void setUsers( ArrayList<User> users) {
        EshopData.users = users;
    }
}

This is my main Frame:

package ui;
import java.awt.event.*;
import java.util.ArrayList;

import javax.swing.*;

import entities.User;

public class Frame extends JFrame{
    
    public static void main(String[] args) {


        //FRAME
        JFrame frame = new JFrame("My first Eshop");

        frame.setSize(300, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        //frame.setVisible(true);
        frame.setLayout(null);
        

        //LABELS
        JLabel l1 = new JLabel("Username:");
        JLabel l2 = new JLabel("Password:");

        l1.setBounds(35, 60, 80, 20);
        l2.setBounds(35, 110, 80, 20);


        //TEXTFIELDS
        JTextField tf1 = new JTextField();
        JTextField tf2 = new JTextField();

        tf1.setBounds(150, 60, 90, 20);
        tf2.setBounds(150, 110, 90, 20);


        //BUTTONS
        JButton b1 = new JButton("Register");
        JButton b2 = new JButton("Login");

        b1.addActionListener(new ActionListener() {
            
            public void actionPerformed(ActionEvent e){

                RegisterFrame rframe = new RegisterFrame();
                rframe.setVisible(true);
                rframe.setSize(600, 600);
            }
        });


        b2.addActionListener(new ActionListener() {
            
            public void actionPerformed(ActionEvent e){
                
            }
        });

        b1.setBounds(35, 150, 100, 20);
        b2.setBounds(160, 150, 80, 20);

        //ADDING TO FRAME
        frame.add(l1);
        frame.add(l2);
        frame.add(tf1);
        frame.add(tf2);
        frame.add(b1);
        frame.add(b2);

        frame.setVisible(true);
    }
    
}

In my main Frame I have a button "Login" (its the 'b2' button) and I want to create an actionListener that will iterate through the array list from above and search for the User.username, if the username exists it will search for the User.password afterwards.
I am not sure whether I am explaining it correctly or not but any kind of help will be appreciated.

If you need any other part of the code I am here to provide it for you. Thnak's in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林迷了鹿 2025-02-13 16:55:22

您可以为ActionListener创建一个自定义包装程序类。此自定义类,我们称其为eshopdatabaseadationlistener将包含eshopdata对象的字段。创建此侦听器时,您将传递已经创建并包含用户列表的eshopdata的实际对象。现在,您可以在侦听器中访问此对象。

将侦听器绑定到登录按钮的下一部分问题现在更容易实现。现在,您可以提供包含您逻辑的ActionPerformed方法的实现。您可以以相同的方式传递用户名和密码。

You can create a custom wrapper class for ActionListener. This custom class, let's call it EshopDataBasedActionListener will contain a field for EshopData object. When you create this Listener, you will pass in the actual Object of EshopData that has already been created and that contains the user list. Now you are able to access this object inside your listener.

The next part of your problem of binding the Listener to your Login button will now be easier to achieve. You can now provide the implementation of the actionPerformed method that will contain your logic. You can pass in the username and password in the same fashion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文