我正在使用JavaFX,我正在尝试从控制器文件添加到一个ArrayList类,所以当在FXML文件上按下创建按钮时,控制器将一个人对象添加到一个联系类,这是一个arrayList但是我希望ArrayList类Contacts是全局的,这样我就可以编辑稍后在
中添加到列表中的任何内容人员类:
package project1;
/**
*
* @author Fomnus
*/
public class Person {
private String firstName;
private String lastName;
/**
* Constructor with parameter
*
* @param firstName
* @param lastName
*/
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName= lastName;
}
/**
* Method to get the name of person
*/
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
/**
* Method to set the name of person
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
Contacts类ArrayList:
package project1;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Fomnus
*/
public class Contacts {
public static void main(String args[]) {
// create an array list
ArrayList contactsList = new ArrayList();
Person person1= new Person("John", "Doe");
Person person2= new Person("Jane", "Rivers");
Person person3= new Person("Steve", "Smith");
Person person4= new Person("Lucy", "White");
contactsList.add(person1);
contactsList.add(person2);
contactsList.add(person3);
contactsList.add(person4);
}
}