1 package javademo;
 2 import java.awt.*;
 3 import javax.swing.*;
 4 import java.awt.event.*;
 5 public class table3{
 6     JFrame frame=new JFrame("表格实例");
 7     Object [][]date= {{"李明",45,"计算机"},{
 8         "小王",32,"数学"},{"老高",35,"通讯"},{
 9             "司机",21,"外语"}
10     };
11     Object []head= {"姓名","年龄","专业"};
12     public void init() {
13         
14     JTable table=new JTable(date,head);
15     JPanel panel=new JPanel(new BorderLayout());
16     panel.add(table.getTableHeader(),BorderLayout.NORTH);
17     panel.add(table,BorderLayout.CENTER);
18     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
19     frame.setContentPane(panel);
20     frame.pack();
21     frame.setLocationRelativeTo(null);
22 frame.show(); 23 24 } 25 public static void main(String []args) { 26 new table3().init(); 27 } 28 }

结果: