【发布时间】:2015-01-05 21:03:05
【问题描述】:
我对 Swing 有点陌生,并且在尝试在 JPanel 中显示 JPanel 数组时遇到了问题。
程序根据用户输入的信息将 Student 对象添加到 Driver 类中的 arraylist 中,然后应该在 studentPane JPanel 中的单独 JPanel 上显示该信息,但在输入信息后它们永远不会显示.
我尝试的第一件事是让 Student 类返回一个 JPanel,方法是创建一个名为 getInfoPane 的方法,该方法返回一个在学生类中创建的 JPanel 及其所有必要信息,然后调用:
studentPane.add(students.get(x).getInfoPane());
但这没有用。我还尝试让 Student 类扩展 JPanel,将学生的信息添加到该面板,然后尝试将其添加到 Driver 类中的 studentPane JPanel 中,但也不显示。
目前,我正在尝试使用名为 StudentInfo 的 JPanel 的单独数组列表,并使用以下代码添加学生:
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 类的信息。这种方法也没有奏效。我已经检查以确保 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;
}
}
所以基本上,我只是想知道为什么程序不会在 studentPane JPanel(位于整个 contentPane 容器内)中显示 JPanel(或组件),以及这样做的正确方法是什么。任何帮助将不胜感激,因为我已经被这个问题困扰了一段时间。
【问题讨论】: