我可以在 Jpanel 构造函数中使用 add(new Jlabel()) 在我的 Jpanel 上创建标签,但是一旦我使用其他函数调用 add(),标签就不会显示在面板上。我做错了什么?
public class DisplayPanel extends JPanel {
JLabel headerField = new JLabel("Choose a file to generate report.");
JLabel dateField = new JLabel("123");
JLabel meanField = new JLabel("");
JLabel minField = new JLabel("");
JLabel maxField = new JLabel("");
JLabel stdDevField = new JLabel("");
public DisplayPanel() {
super();
setBackground(Color.white);
setLayout(new GridLayout(6, 1));
add(headerField);
**//add(new JLabel("123")); this will work**
}
public void setFields(DataManager d)
{
dateField.setText(d.getStartDate() + " - " + d.getEndDate());
meanField.setText("Mean: " + d.getMean());
minField.setText("Min: " + d.getMin());
maxField.setText("Max: " + d.getMax());
stdDevField.setText("Std Dev: " + d.getStdev());
this.add(new JLabel("123")); **//this doesn't work**
}