如何使JTable使用父面板的全部可用内容?我试过使用setSize或setPreferredSize,但是没有用。后者实际上产生了桌子的很小的版本。
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.insets = new Insets(0, 0, 25, 15);
String[] columnNames = {"A", "B", "C", "D", "E"};
Object[][] data = new Object[20][columnNames.length];
for( int i = 0; i < 20; i++ ){
data[i] = new Object[]{"Some other stuff that is long", "K", "1234567891011121314", "R", "T"};
}
JTable skillTable = new JTable(data, columnNames);
skillTable.setFillsViewportHeight(true );
JScrollPane scrollTable = new JScrollPane(skillTable);
constraints.fill = constraints.HORIZONTAL;
panel.add(scrollTable, constraints);
外观如下:http://i.stack.imgur.com/P4b6A.png
最佳答案
与BorderLayout整齐而简单
JPanel panel = new JPanel(new BorderLayout());
String[] columnNames = {"A", "B", "C", "D", "E"};
Object[][] data = new Object[20][columnNames.length];
for (int i = 0; i < 20; i++) {
data[i] = new Object[]{"Some other stuff that is long", "K", "1234567891011121314", "R", "T"};
}
JTable skillTable = new JTable(data, columnNames);
skillTable.setFillsViewportHeight(true);
JScrollPane scrollTable = new JScrollPane(skillTable);
panel.add(scrollTable, BorderLayout.CENTER);
//as you said you want to add a button as well
JPanel buttonpanel=new JPanel();
buttonpanel.add(new JButton("Test"));
panel.add(buttonpanel,BorderLayout.SOUTH);