JTable組件顯示數據時,如果直接將其放置在Frame的contentPane中則表頭一行會顯示不出來,如果將其放置在JScrollPane中顯示數據的話,表頭會自動顯示出來。
importjavax.swing.JFrame;
importjavax.swing.JScrollPane;
importjavax.swing.JTable;
publicclasstesttable {
staticJFrame jf = newJFrame();
publicstaticvoidmain(String[] args) {
String[] name={"姓名","號碼"};
String[] s1 = { "小王", "213213"};
String[] s2 = { "小芳", "142321"};
String data[][] = { s1, s2 };
JTable table = newJTable(data,name);
JScrollPane JSP= newJScrollPane(table);
jf.add(JSP);
jf.setTitle("練習");
jf.setBounds(300, 300, 300, 300);
jf.setVisible(true);
}
}