我一直在写一个构造函数。创建表。但是我希望它在桌子上有滚动条。我一直在大力搜索如何正确地做到这一点。然而,它似乎并没有起作用。我不确定我哪里出错了。
public ApplicationPanel(String[][] data, String[] colNames)
{
JTable table = new JTable(data, colNames);
table.setPreferredScrollableViewportSize(new Dimension(450, 65));
table.setFillsViewportHeight(true);
TableColumn column;
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setPreferredSize(new Dimension(900,800));
// column.setResizable(true);
for (int i = 0; i<4; i++)
{
column = table.getColumnModel().getColumn(i);
if (i == 2)
{
column.setPreferredWidth(400);
}
else
{
column.setPreferredWidth(50);
}
}
this.add(scrollPane);
}
答案 0 :(得分:0)
将区域填充为@ alex2410建议,并将以下代码添加到构造函数中。
this.setLayout(new GridLayout());
之后可以查看表滚动条。 将此与上面的代码相结合(减去下面的代码,您应该有一个带滚动条的工作表构造函数。
以下代码已删除,并且在某些时候是代码中的代码。因此,作为参考,这些似乎没有任何影响,我可以告诉你。
table.setPreferredScrollableViewportSize(new Dimension(450, 65));
table.setFillsViewportHeight(true);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
scrollPane = new JScrollPane(table,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
ApplicationFrame.getFrames().getContentPane().add( new JScrollPane( table ), BorderLayout.CENTER );
column.setResizable(true);