[英]Displaying a list of people, after getting information about them, Java, GUI
I have a class called "Person": 我有一堂课叫做“人”:
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;
}
}
And I would like to be able to add a person to an array which should then be displayed. 而且我希望能够将一个人添加到随后应显示的数组中。 The code I have written so far looks like this: 到目前为止,我编写的代码如下:
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
}
, but I have a couple of problems: ,但有两个问题:
Thank you! 谢谢!
Instead of 代替
Person[] personList = new Person [];
use 采用
List<Person> personList = new ArrayList<>();
You can add persons by 您可以按以下方式添加人员
personList.add(new Person(...));
Using list instead of array can solve the problem of having to specify the size at time of initializing the variable. 使用列表而不是数组可以解决在初始化变量时必须指定大小的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.