我目前正在为我的学校制作一个点数系统,它从数据库中提取数据并将其显示在DataPanel(JPanel的子类)中,该子类填充了Student类,每个类都是单独的单元格。这也是JPanel的子类。我之所以不使用JTable,是因为它不能满足我的审美需求。无论如何,当我只是将Student类添加到DataPanel时,它工作正常,但是,当我尝试通过创建一个具有布局BorderLayout的父JPanel并添加West上的复选框和中心的Student时添加JCheckBox,它只显示一个单元格,即使DataPanel的大小应该包含数据库中的所有数据。以下是一些图片和代码:
以下是DataPanel类的代码:
class DataPanel extends JPanel{
static Student[] students;
public DataPanel(String[][] data) {
try{setLayout(new GridLayout(data.length,1));} catch(Exception e) {
GUI.control.updateUI();
JOptionPane.showMessageDialog(null, "The search criteria you have entered is incorrect and did not generate any outcome.", "Invalid Search Criteria", JOptionPane.ERROR_MESSAGE);
}
setPreferredSize(new Dimension(382,data.length*20));
students = new Student[data.length-1];
JPanel parent = new JPanel(new BorderLayout());
for(int i = 0; i < data.length; i++) {
Student contents = new Student();
JLabel[] labels = new JLabel[4];
for(int x = 0; x < data[0].length; x++) {
JLabel cur = new JLabel(" "+data[i][x]);
cur.setFont(new Font("Arial", Font.PLAIN,12));
labels[x] = cur;
//System.out.println(data[0].length);
contents.add(cur);
}
contents.setId(labels[0]);
contents.setName(labels[1]);
contents.setForm(labels[2]);
contents.setPoints(labels[3]);
contents.setParent(parent);
JCheckBox box = new JCheckBox();
contents.setCheckBox(box);
parent.setBorder(BorderFactory.createLineBorder(Color.lightGray));
parent.add("Center", contents);
parent.add("West", box);
add(contents); // >> parent = only one showing?
System.out.println(i);
if(i == 0) {
parent.setBackground(new Color(200,200,255));
}
if(i != 0) {
students[i-1] = contents;
}
}
}
public static void refresh() {
for(Student i: students) {
if(i != Student.getSelected()) {
i.setBackground(Color.white);
}
}
}
}
和学生班:
class Student extends JPanel implements MouseListener {
JLabel name;
JLabel id;
JLabel points;
JLabel form;
JCheckBox box;
JPanel parent;
private static Student selected;
public Student() {
super(new GridLayout());
addMouseListener(this);
}
public void setParent(JPanel parent) {
this.parent = parent;
parent.setBackground(Color.white);
}
public void setCheckBox(JCheckBox box) {
this.box = box;
}
public void setName(JLabel name) {
this.name = name;
}
public void setId(JLabel id) {
this.id = id;
}
public void setPoints(JLabel points) {
this.points = points;
}
public void setForm(JLabel form) {
this.form = form;
}
@Override
public void mouseClicked(MouseEvent e) {
if(name.getText().equals(" Student")) {
parent.setBackground(new Color(200,200,255));
return;
}
parent.setBackground(new Color(255,255,190));
setSelected(this);
DataPanel.refresh();
}
@Override
public void mouseEntered(MouseEvent e) {
if(name.getText().equals(" Student")) {
parent.setBackground(new Color(200,200,255));
return;
}
if(this == getSelected()) {
parent.setBackground(new Color(255,255,190));
} else {
parent.setBackground(new Color(230,230,255));
}
}
@Override
public void mouseExited(MouseEvent e) {
if(name.getText().equals(" Student")) {
parent.setBackground(new Color(200,200,255));
return;
}
if(this == getSelected()) {
parent.setBackground(new Color(255,255,210));
} else {
parent.setBackground(Color.white);
}
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
public static Student getSelected() {
return selected;
}
public static void setSelected(Student selected) {
Student.selected = selected;
}
}
提前感谢您的帮助,并告诉我您是否需要更多信息......
哦,顺便说一句,不注意实际的数据,只是出于测试目的:)
编辑:只有当我将parent
(JPanel)添加到DataPanel而不是contents
(JPanel)时才会出现问题。
答案 0 :(得分:0)
我找到了答案。 实际上非常愚蠢。
嗯,基本上,我有JPanel&#39; parent&#39;的构造函数。 外部 for循环,因此它只创建一个并将全部数据添加到JPanel中。这就是为什么只有一个出现的原因。 感谢所有试图帮助我的人。遗憾。