我對Swing有點新,並且在嘗試在JPanel內顯示JPanel的數組時遇到了問題。在JPanel中顯示JPanel的數組
該程序將Student對象添加到基於用戶輸入的信息的Driver類中的ArrayList中,然後應該將該信息顯示在studentPane JPanel中的單獨JPanel上,但是在放入信息後它們不會顯示在
我想是有這個學生類通過製造所謂的getInfoPane方法,它返回與所有必要的信息,學生類創建一個JPanel,然後調用返回的JPanel的第一件事:
studentPane.add(students.get(x).getInfoPane());
但這沒有奏效。我也曾嘗試讓Student類擴展JPanel,將該學生的信息添加到該面板,然後嘗試將其添加到Driver類中的studentPane JPanel中,該類不會顯示該類。
目前,我想有JPanels稱爲StudentInfo單獨的ArrayList,以及與此代碼添加學生:
for(int x=0; x<Integer.parseInt(numberField.getText()); x++) {
studentInfo.add(new JPanel()); //add new JPanel to array
studentInfo.get(x).add(new JLabel (students.get(x).toString())); //add info from Student
studentPane.add(studentInfo.get(x)); //add JPanel with student info to main Student JPanel
}
與toString方法被重寫與輸入到該信息返回一個字符串學生班。這種方法也沒有奏效。我已經檢查過,以確保Student類正在接收放入的信息,並將其打印出來。我也嘗試直接將組件(如JLabel)添加到Driver類中的studentPane,但它不起作用。經過一些測試後,我發現字符串是唯一會顯示在studentPane上的東西。這導致我認爲問題處理的不是從另一個類傳遞JPanel。我看過很多類似問題的其他問題,但無法使解決方案適用於我的程序。
到目前爲止,這裏是從我的程序相關代碼:
public class Driver extends JFrame {
private Container contentPane;
ArrayList<Student> students = new ArrayList<Student>();
private JPanel studentPane = new JPanel();
ArrayList<JPanel> studentInfo = new ArrayList<JPanel>();
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Driver frame = new Driver();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Driver() {
contentPane = this.getContentPane();
profiles.add(addStudentProfiles); // this chunk has the user input student info and tries to add it to studentPane
addStudentProfiles.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, number, "Number to create", JOptionPane.INFORMATION_MESSAGE);
if(numberField.getValue() != null) {
for(int x=0; x<Integer.parseInt(numberField.getText()); x++) {
while(nameField.getText().equals("") || workEthicField.getText().equals("")) {
JOptionPane.showMessageDialog(null, studentInputs, "New Student", JOptionPane.PLAIN_MESSAGE);
if(nameField.getText().equals("") || workEthicField.getText().equals(""))
JOptionPane.showMessageDialog(null, "Error. Please fill out all of the fields");
}
students.add(new Student(nameField.getText(), Integer.parseInt(workEthicField.getText()), (String)(major1Field.getSelectedItem())));
nameField.setText(""); workEthicField.setValue(null);
}
for(int x=0; x<Integer.parseInt(numberField.getText()); x++) {
studentInfo.add(new JPanel());
studentInfo.get(x).add(new JLabel (students.get(x).toString()));
studentPane.add(studentInfo.get(x));
}
numberField.setValue(null);
}
}
});
setContentPane(contentPane);
contentPane.setLayout(null);
studentPane.setBorder(BorderFactory.createTitledBorder(BorderFactory.createMatteBorder(3, 3, 3, 3, Color.black), "Students", TitledBorder.CENTER, TitledBorder.CENTER));
studentPane.setLocation(350, 0);
studentPane.setSize(300, (int)getBounds().getHeight()-62);
studentPane.setBackground(Color.lightGray);
studentPane.setVisible(true);
contentPane.add(studentPane);
}
}
這裏是學生類:
public class Student {
private String name, major;
private int workEthic;
private String info = "";
public Student(String name, int workEthic, String major) {
this.name = name;
this.workEthic = workEthic;
this.major = major;
info = name+ "\n" +major+ "\n" +workEthic;
}
public String toString() {
return info;
}
}
所以基本上,我只是想知道爲什麼程序將不顯示JPanel(或組件),在studentPane JPanel(它位於整個contentPane容器內部)中,以及正確的方法是什麼。任何幫助將不勝感激,因爲我一直在這個問題上停留了一段時間。