我遇到了一个罕见的问题。当我将代码放在按钮的事件中时,它可以工作并且表格可以正确显示,但是当我将它放在initComponents()
函数下方(在构造函数内部)时,它不起作用。
String[] columnNames = {"Nick", "E-Mail"};
Object[][] data = {
{"Kathy", "abc@hotmail.com"},
{"John", "Doe"},
{"Sue", "Black"},
{"Jane", "White"},
{"Joe", "Brown"}
};
jPanel1.setLayout(new BorderLayout());
JTable table = new JTable(data, columnNames);
Dimension dim = new Dimension(jPanel1.getWidth(),jPanel1.getHeight());
JScrollPane tableScroll = new JScrollPane(table);
tableScroll.setPreferredSize(dim);
jPanel1.add(tableScroll, BorderLayout.CENTER);
jPanel1.validate();
有任何想法吗?