我有一个名为“人”的课程:
package personFiles;
public class Person {
private int id;
private String name;
private String surname;
private int age;
private String gender;
public int getID() {
return id;
}
public void setID(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Person(int id, String name, String surname, int age, String gender) {
this.id = id;
this.name = name;
this.surname = surname;
this.age = age;
this.gender = gender;
}
public String toString() {
return id + "; " + name + "; " + surname + "; " + age + "; " + gender;
}
}
我希望能够将一个 person 添加到一个应该显示的数组中。 到目前为止我写的代码看起来像这样:
package personFiles;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
@SuppressWarnings("serial")
public class Osoblje extends JFrame {
private JTextArea outputJTA = new JTextArea("");
private JScrollPane outputJTAScrollPane = new JScrollPane(outputJTA);
private JLabel idJL = new JLabel();
private JLabel nameJL = new JLabel();
private JTextField idJTF = new JTextField();
private JTextField nameJTF = new JTextField();
private JLabel surnameJL = new JLabel();
private JLabel ageJL = new JLabel();
private JTextField surnameJTF = new JTextField();
private JTextField ageJTF = new JTextField();
private JLabel genderJL = new JLabel();
private JTextField genderJTF = new JTextField();
private JButton newPersonJB = new JButton();
private JButton pokaziJB = new JButton();
Person[] personList = new Person [];
public Osoblje(String title) {
// Frame-Initialisierung
super(title);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
int frameWidth = 310;
int frameHeight = 269;
setSize(frameWidth, frameHeight);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int x = (d.width - getSize().width) / 2;
int y = (d.height - getSize().height) / 2;
setLocation(x, y);
setResizable(false);
Container cp = getContentPane();
cp.setLayout(null);
// Anfang Komponenten
outputJTAScrollPane.setBounds(8, 8, 129, 217);
cp.add(outputJTAScrollPane);
idJL.setBounds(152, 16, 51, 19);
idJL.setText("ID:");
cp.add(idJL);
nameJL.setBounds(152, 40, 54, 20);
nameJL.setText("Name:");
cp.add(nameJL);
idJTF.setBounds(208, 16, 65, 17);
cp.add(idJTF);
nameJTF.setBounds(208, 40, 65, 17);
cp.add(nameJTF);
surnameJL.setBounds(152, 64, 59, 19);
surnameJL.setText("Surname:");
cp.add(surnameJL);
ageJL.setBounds(152, 88, 51, 19);
ageJL.setText("Age:");
cp.add(ageJL);
surnameJTF.setBounds(208, 64, 65, 17);
cp.add(surnameJTF);
ageJTF.setBounds(208, 88, 65, 17);
cp.add(ageJTF);
genderJL.setBounds(152, 112, 59, 19);
genderJL.setText("Gender:");
cp.add(genderJL);
genderJTF.setBounds(208, 112, 65, 17);
cp.add(genderJTF);
newPersonJB.setBounds(152, 144, 121, 25);
newPersonJB.setText("nova osoba");
newPersonJB.setMargin(new Insets(2, 2, 2, 2));
newPersonJB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
newPersonJB_ActionPerformed(evt);
}
});
cp.add(newPersonJB);
pokaziJB.setBounds(152, 184, 121, 25);
pokaziJB.setText("pokazi");
pokaziJB.setMargin(new Insets(2, 2, 2, 2));
pokaziJB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
pokaziJB_ActionPerformed(evt);
}
});
cp.add(pokaziJB);
// Ende Komponenten
setVisible(true);
} // end of public Osoblje
// Anfang Methoden
public static void main(String[] args) {
new Osoblje("Osoblje");
} // end of main
public void newPersonJB_ActionPerformed(ActionEvent evt) {
personList[i] = new Person(idJTF.getText(), nameJTF.getText(), surnameJTF.getText(), ageJTF.getText(), genderJTF.getText()); //create new person
} // end of newPersonJB_ActionPerformed
public void pokaziJB_ActionPerformed(ActionEvent evt) {
outputJTA.setText("PersonID; Name; Surname; Age; Gender \n" + personList[i] + "\n"); //display personList
} // end of pokaziJB_ActionPerformed
}
,但我有几个问题:
谢谢!
答案 0 :(得分:2)
而不是
Person[] personList = new Person [];
使用
List<Person> personList = new ArrayList<>();
您可以按
添加人员personList.add(new Person(...));
使用list而不是array可以解决在初始化变量时必须指定大小的问题。