我创建了一个包含按钮的“frame1”,所以如果我点击该按钮,按钮动作执行的方法将检查LinkedList是否为空,如果是,它将设置“addEmployee”框架可见,其中包含radioButton调用addEmployees_Auto,当单击时,它将从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));
}
}
AddEmployee frame:
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);
}
}
答案 0 :(得分:0)
我认为这是因为你在做:
new Frame1()
每次都在你的AddEmployees
课程中,所以每次创建一个新的链表时,总是空的。您需要传入对Frame1
的调用实例的引用,例如
new AddEmployees(this)
来自Frame1
类,并将适当的构造函数添加到AddEmployees
类中,例如
public AddEmployees(JFrame parent) { ... }