JTable表头不显示的问题

import javax.swing.*;
import java.awt.*;

public class TableDemoMVC extends JFrame
{
	TableDemoMVC(){
		init();
	}
	protected void init(){
		Container ct;
		final String[] columnNames = {"姓名","职位","电话","月薪","婚否"};
		//表格中各行的内容保存在二维数组data中
		final Object[][] data = {
			{"王东","总经理","0101",new Integer(5000),new Boolean(false)},
			{"李宏","秘书","0102",new Integer(3500),new Boolean(true)},
			{"李瑞","开发","0103",new Integer(4500),new Boolean(false)},
			{"赵新","保卫","0104",new Integer(2000),new Boolean(true)},
			{"陈理","销售","0105",new Integer(4000),new Boolean(false)}
		};
		//创建表格
		JTable table = new JTable(data,columnNames);
		//将表格加入滚动窗口
		this.setSize(new Dimension(400,130));
		JScrollPane jp = new JScrollPane(table);
		ct = getContentPane();
		ct.add(jp,BorderLayout.CENTER);                //如果直接将table放入容器ct中,表头不会显示
	} 
	public static void main(String[] args) throws ClassNotFoundException,
		InstantiationException,IllegalAccessException,
		UnsupportedLookAndFeelException
	{
		UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		TableDemoMVC frame = new TableDemoMVC();
		//frame.pack()
		frame.setVisible(true);
	}
}

第26行的语句,如果直接将table放入容器ct中,表格的表头不会显示。而是应该先将表格放入JScrollPane对象中,再将该对像放入容器ct中。

猜你喜欢

转载自blog.csdn.net/seabiscuityj/article/details/80713019