如图 表格未显示数组tt中的数据为什么呢?但是如果面板容器用JScrollPane就可以全部显示。 小白求解
[code="java"]
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[][] values = { { "aaa", "aaa", "aaa" }, { "bbb", "bbb", "bbb" },
{ "ccc", "ccc", "ccc" } };
String[] titles = { "Id", "Name", "Note" };
JTable table = new JTable(values, titles);
JScrollPane scrollpane = new JScrollPane(table);
frame.add(scrollpane);
frame.pack();
frame.setVisible(true);
}
[/code]
注意,如果要在单独的视图中(在 JScrollPane 外)使用 JTable 并显示表标题,则可以使用 getTableHeader() 获取并单独显示它。
[code="java"]
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[][] values = { { "aaa", "aaa", "aaa" }, { "bbb", "bbb", "bbb" },
{ "ccc", "ccc", "ccc" } };
String[] titles = { "Id", "Name", "Note" };
JTable table = new JTable(values, titles);
// JScrollPane scrollpane = new JScrollPane(table);
frame.add(table.getTableHeader(),BorderLayout.NORTH);
frame.add(table,BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
[/code]
The scroll pane automatically places the table header at the top of the viewport. The column names remain visible at the top of the viewing area when the table data is scrolled.
If you are using a table without a scroll pane, then you must get the table header component and place it yourself. For example:
container.setLayout(new BorderLayout());
container.add(table.getTableHeader(), BorderLayout.PAGE_START);
container.add(table, BorderLayout.CENTER);