0

我创建了一个包含一个按钮的“frame1”,所以如果我点击那个按钮,按钮操作执行的方法将检查 LinkedList 是否为空,如果是,它将设置包含 radioButton 调用 addEmployees_Auto 的“addEmployee”框架可见,并且单击时,它将从 frame1 调用方法“add_Auto”。这应该填充frame1中的linkedList,但如果我再次单击按钮,LinkedList又是空的。为什么?LinkedList 不应该被填充吗?

这是“frame1”

public class Frame1 extends javax.swing.JFrame {

    LinkedList <Employee>list2=new LinkedList <Employee>();
        ....

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

    System.out.println("size="+list2.size());

    if(list2.isEmpty())
      new AddEmployees.setVisible(true);
}

public void addEmployees_Auto()
     {

        list2.add(new Employee(20910733,"Ahmed","Manager",2700));
        list2.add(new Employee(20910835,"Omar","Teaacher",2100));
        list2.add(new Employee(20910674,"Mohammed","Manager",3000));
        list2.add(new Employee(20910955,"Kareem","",2700));
        list2.add(new Employee(20910921,"Ameer","teacher",2500));

     }
}

添加员工框架:

public class AddEmployees extends javax.swing.JFrame {
             ...

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)    
 {                                         

     if(jRadioButton2.isSelected()){

        new Frame1().addEmployees_Auto();
        this.setVisible(false);
  }

}       
4

1 回答 1

0

我认为这是因为你正在做:

new Frame1()

每次在你的AddEmployees课堂上,所以每次都会创建一个新的链表,它总是为空的。您需要传入对调用实例的引用Frame1,例如

new AddEmployees(this)

Frame1类中,并将适当的构造函数添加到您的AddEmployees类中,例如

public AddEmployees(JFrame parent) { ... }
于 2013-04-09T18:44:48.747 回答