JTable组件显示数据时,如果直接将其放置在Frame的contentPane中则表头一行会显示不出来,如果将其放置在JScrollPane中显示数据的话,表头会自动显示出来。
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class testtable {
static JFrame jf = new JFrame();
public static void main(String[] args) {
String[] name={"姓名","号码"};
String[] s1 = { "小王", "213213" };
String[] s2 = { "小芳", "142321" };
String data[][] = { s1, s2 };
JTable table = new JTable(data,name);
JScrollPane JSP= new JScrollPane(table);
jf.add(JSP);
jf.setTitle("练习");
jf.setBounds(300, 300, 300, 300);
jf.setVisible(true);
}
}