Java Swing Jtable的简单应用
代码如下:
import java.awt.EventQueue;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class Jtable和list的简单应用 extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTable table;
//需要自定义列表数据
Object[] 列表的抬头数据= {"日期","课程名称","教室","课程老师","时长"};
Object[][] 列表的每行的数据= {
{"周一","美学基础","101","颜美","90分钟"},
{"周二","语言学","304","言旭东","90分钟"},
{"周三","基础化工","205","廖化","90分钟"},
{"周四","计算机科学","503","计西中","90分钟"},
{"周五","制造学","401","梅德银","90分钟"},
{"周六","交际学","大舞台","武工","90分钟"},
{"周日","养生学","大厅","陈仙","90分钟"},
};
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Jtable和list的简单应用 frame = new Jtable和list的简单应用();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Jtable和list的简单应用() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 395, 170);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(12, 12, 370, 122);
contentPane.add(scrollPane);
//此处定义table参数
table = new JTable(列表的每行的数据, 列表的抬头数据);
scrollPane.setViewportView(table);
}
}
最终的显示效果如下: