在获得有关人员、Java、GUI的信息后,显示人员列表
我有一门课叫“人”: 我希望能够将一个人添加到一个数组中,然后显示这个数组。 到目前为止,我编写的代码如下所示:在获得有关人员、Java、GUI的信息后,显示人员列表,java,arrays,list,user-interface,arraylist,Java,Arrays,List,User Interface,Arraylist,我有一门课叫“人”: 我希望能够将一个人添加到一个数组中,然后显示这个数组。 到目前为止,我编写的代码如下所示: package personFiles; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; @SuppressWarnings("serial") public class Osoblje extends JFrame { priva
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
}
,但我有几个问题:
Person[] personList = new Person [];
使用
使用列表而不是数组可以解决在初始化变量时必须指定大小的问题。如果您在开始时不知道数组的大小,最好使用列表来添加人员。这需要大量代码。你有什么办法吗?很好的建议。再多解释一下它的优点或者它是如何解决提问者的问题也会很好。非常感谢!我试着照你说的做。但是:当我写个人列表时,加上(1,“jkdsaö”,“dai”,12,“男性”);我收到一条错误消息:类型列表中的add(int,Person)方法不适用于参数(int,String,String,int,String)为什么?如何去掉输出中的方括号?personList.get(index)
Person[] personList = new Person [];
List<Person> personList = new ArrayList<>();
personList.add(new Person(...));