我对此很新,主要是自学,所以任何建议都会对我有所帮助。我已经对此进行了抨击,现在看起来我已经开始了......
所以我使用netbeans GUI构建器创建了一个GUI ...我有一个主菜单,我希望能够从任何框架修改一个arraylist ......
我有一个员工对象,我想进入一个可以从任何类文件访问的ArrayList。我在这个网站上寻找了一些帮助,但最终试图弄清楚如何将它应用到我自己身上。
public class EmployeesDATA {
private ArrayList<Employee> EmployeesDATA;
Main m; //not so sure what this is meant to do
public EmployeesDATA(Main m) {
EmployeesDATA = new ArrayList<>();
this.m = m;
}
public ArrayList<Employee> getList() {
return EmployeesDATA;
}
public void addEmployee(Employee e){
EmployeesDATA.add(e);
}
}
这是我开始变得非常困惑的地方......结束让这个类试图模仿其他人的建议大声笑。
public class Main {
public static EmployeesDATA Employees;
public Main() {
//test employee
Employee TestObject = new Employee("Jeff","b",1);
//make a new list using this class file only.
Employees = new EmployeesDATA(this);
//add a employee to the list.
Employees.addEmployee(TestObject);
}//end method
public void addEmployeeMainList(Employee e) {
Employees.addEmployee(e);
System.out.println("added employee.");
}
我认为我试图得到的是一个只创建一次然后在其他地方调用的ArrayList。我很早就尝试创建新的ArrayLists并丢失旧数据。
接下来是我的MainMenu类,它是创建GUI的地方。主要方法是在这里,但我还没有触及它,因为它已经有很多其他生成的东西:
public class MainMenu extends javax.swing.JFrame {
private EmployeesDATA Employees;
public MainMenu() {
initComponents(); //generated
System.out.println(Main.Employees.getList().get(0).employeeFirstName + "test 1"); // I seem to crash out here...
//pretty confused, I can call the Main class but I can't get the Employees.addEmployeeMainList method which is where I wanted to try to keep it protected... Any help there?
//eventually I will move on to assigning stuff to my GUI items like this:
//Populate Combo Box with Employees.
for(int x = 0; x < Employees.size(); x++){
SelectEmployeeComboBox.addItem(Employees.get(x).employeeFirstName);
}}
我真的只是在这一点上让自己感到困惑,我很确定有一种更为简单的方法可以解决这个问题。 任何指针,即使它没有解决手头的问题。在这里学习:)。
答案 0 :(得分:1)
抛弃Main
类 - 根本没有意义,试试这个
public class MainMenu extends javax.swing.JFrame {
private EmployeesDATA employees = new EmployeesDATA();
public MainMenu() {
initComponents(); //generated
Employee testObject = new Employee("Jeff","b",1);
employees.addEmployee(testObject);
System.out.println(employees.getList().get(0).employeeFirstName + "test 1");
for(int x = 0; x < Employees.size(); x++){
SelectEmployeeComboBox.addItem(Employees.get(x).employeeFirstName);
}}
答案 1 :(得分:-1)
不确定您想要做什么,但是如果您希望将所有数据保存在列表中并且能够从任何地方执行此操作并控制可以执行的操作
class Data{
private data(){};
private static final ArrayList<Employee> elist =new ArrayList<Employee>();
public static void add(Employee e){
elist.add(e)
}
//search..
//get a copy of list..
//watever..
}
从任何地方都可以调用Data.add(e)