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);
}
}