为什么以下java代码jtable不显示表头?
importjavax.swing.JFrame;importjavax.swing.JTable;publicclasstesttable{staticJFramejf...
import javax.swing.JFrame;
import javax.swing.JTable;
public class testtable {
static JFrame jf = new JFrame();
public static void main(String[] args) {
JTable table = null;
String[] name={"姓名","号码"};
String[] s1 = { "小王", "213213" };
String[] s2 = { "小芳", "142321" };
String data[][] = { s1, s2 };
table = new JTable(data,name);
jf.add(table);
jf.setTitle("练习");
jf.setBounds(300, 300, 300, 300);
jf.setVisible(true);
}
} 展开
import javax.swing.JTable;
public class testtable {
static JFrame jf = new JFrame();
public static void main(String[] args) {
JTable table = null;
String[] name={"姓名","号码"};
String[] s1 = { "小王", "213213" };
String[] s2 = { "小芳", "142321" };
String data[][] = { s1, s2 };
table = new JTable(data,name);
jf.add(table);
jf.setTitle("练习");
jf.setBounds(300, 300, 300, 300);
jf.setVisible(true);
}
} 展开
1个回答
展开全部
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);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询