简体   繁体   English

尽管添加了元素,但是链表为空

[英]linkedList is empty although elements added to it

I created a "frame1" that contains a button , so if I click on that button , button action performed method will check if the LinkedList is empty , if is ,it will set the "addEmployee" frame visible which contains radioButton calls addEmployees_Auto, and when clicked , it will invoke method "add_Auto" from frame1 . 我创建了一个包含一个按钮的“ frame1”,因此,如果我单击该按钮,则执行按钮操作的方法将检查LinkedList是否为空,如果是,它将设置“ addEmployee”框架可见,其中包含radioButton调用addEmployees_Auto,并且单击时,它将从frame1调用方法“ add_Auto”。 This should fill the linkedList in frame1, but if I re-clicked on the button again , the LinkedList is empty again. 这应该填充frame1中的linkedList,但是如果我再次单击该按钮,LinkedList再次为空。 why? 为什么? shouldn't the LinkedList be fill ?? 不应该填充LinkedList吗?

this is "frame1" 这是“ 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));

     }
}

AddEmployee frame : AddEmployee框架:

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);
  }

}       

I think this is because you are doing: 我认为这是因为您正在执行以下操作:

new Frame1()

every time in your AddEmployees class, so each time a new linked list is being created, which is always going to be empty. 每次在AddEmployees类中,因此每次创建新的链表时,该链表始终为空。 You need to pass in a reference to the calling instance of Frame1 , such as 您需要传递对Frame1调用实例的引用,例如

new AddEmployees(this)

from the Frame1 class, and add the appropriate constructor to your AddEmployees class, eg Frame1类,然后将适当的构造函数添加到您的AddEmployees类,例如

public AddEmployees(JFrame parent) { ... }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM