0

我遇到了一个罕见的问题。当我将代码放在按钮的事件中时,它可以工作并且表格可以正确显示,但是当我将它放在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();

有任何想法吗?

4

1 回答 1

3
  • 代替Dimension dim = new Dimension(jPanel1.getWidth(),jPanel1.getHeight());

  • 因为之前JFrame.pack()orJFrame.setVisible(true)返回零维度 [0, 0]


  • table.setPreferredScrollableViewportSize(table.getPreferredSize());

于 2013-08-25T18:19:40.713 回答