package test.swing.jtable;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
public class TestDefaultTableModel extends JFrame {
public static void main(String[] args) {
TestDefaultTableModel frame = new TestDefaultTableModel();
frame.setVisible(true);
}
public TestDefaultTableModel(){
setTitle("Test DefaultTableModel for version 1.0.1");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 500); //(左邊界,上邊界,宽度,長度)
JPanel contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(10, 10, 10, 10)); //(上,左,下,右)
setContentPane(contentPane);
//設置Layout的樣式來呈現component之間在Panel上的位置關係
contentPane.setLayout(new BorderLayout());
//第一種方式: 為table初始宣告一些測試資料 (常用方法1)
JTable table = new JTable(new Object[][]{
{"aaa000","aaa001","aaa002"},
{"bbb000","bbb001","bbb002"},
{"ccc000","ccc001","ccc002"}},
new Object[]{"Title A","Title B","Title C"});
//第二種方式: 為table初始宣告一些測試資料 (常用方法2)
/*Object[][] dataVector = new Object[][]{
{"xxx000","xxx001","xxx002"},
{"yyy000","yyy001","yyy002"},
{"zzz000","zzz001","zzz002"}};
Object[] columnIdentifiers = new Object[]{"Title X","Title Y","Title Z"};
DefaultTableModel model = new DefaultTableModel();
model.setDataVector(dataVector, columnIdentifiers);
JTable table = new JTable(model);*/
//第三種方式: 逐一加入每一欄位名稱與該欄位的所有記錄 (為table初始宣告一些測試資料)
/*DefaultTableModel model = new DefaultTableModel();
model.addColumn("columnName0", new Object[]{"cn000","cn003","cn006"});
model.addColumn("columnName1", new Object[]{"cn001","cn004","cn007"});
model.addColumn("columnName2", new Object[]{"cn002","cn005","cn008"});
JTable table = new JTable();
table.setModel(model);*/
//第四種方式: 以Vector逐一加入每一欄位名稱與該欄位的所有記錄 (為table初始宣告一些測試資料)
/*DefaultTableModel model = new DefaultTableModel();
Vector cnv = new Vector();
cnv.add("cln000");
cnv.add("cln001");
model.addColumn(new String("columnName0"), cnv);
cnv = new Vector();
cnv.add("cln002");
cnv.add("cln003");
model.addColumn(new String("columnName1"), cnv);
cnv = new Vector();
cnv.add("cln004");
cnv.add("cln005");
model.addColumn(new String("columnName2"), cnv);
JTable table = new JTable(model);*/
//第五種方式: 先加人欄位,後加入資料 (為table初始宣告一些測試資料)
/*DefaultTableModel model = new DefaultTableModel();
model.setColumnIdentifiers(new Object[]{"columnName0","columnName1", "columnName2"});
model.addRow(new Object[]{"coln000","coln001","coln002"});
model.addRow(new Object[]{"coln003","coln004","coln005"});
model.addRow(new Object[]{"coln006","coln007","coln008"});
JTable table = new JTable();
table.setModel(model);*/
contentPane.add(new JScrollPane(table), BorderLayout.CENTER);
}
}
|
c计算机编程
回覆刪除c实现格里高利历的例子