通过另一个类问题的数组列表迭代
这是我存储数据的类:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以为
ActionListener
创建一个自定义包装程序类。此自定义类,我们称其为eshopdatabaseadationlistener
将包含eshopdata
对象的字段。创建此侦听器时,您将传递已经创建并包含用户列表的eshopdata
的实际对象。现在,您可以在侦听器中访问此对象。将侦听器绑定到登录按钮的下一部分问题现在更容易实现。现在,您可以提供包含您逻辑的
ActionPerformed
方法的实现。您可以以相同的方式传递用户名和密码。You can create a custom wrapper class for
ActionListener
. This custom class, let's call itEshopDataBasedActionListener
will contain a field forEshopData
object. When you create this Listener, you will pass in the actual Object ofEshopData
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.