JScrollPane 大于 JTable 行

JScrollPane is larger than JTable row

我使用此语句将 scrollPane 分配给 Jtable。但是,当我将 scrollPane 添加到任何其他 JPanel 时。 scrollPane 的高度远大于 table 高度。

JPanel tablepane=new JPanel();
tableau= new JTable();
tableau.setModel(model);
tableC=new JScrollPane();
tableC.setViewportView(tableau);

如果你想要它的确切尺寸,那么你可以使用:

table.setPreferredScrollableViewportSize(table.getPreferredSize());

将数据加载到 table 之后。