点击打开链接这个上面有很多样例
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class test {
public static void main(String[] argv) throws Exception {
DefaultTableModel model = new DefaultTableModel();
JTable table = new JTable(model);
model.addColumn("Col1");
model.addColumn("Col2");
model.addRow(new Object[] { "v1", "v2" });
//按照列的序列添加数据到表
model.addRow(new Object[] { "v1" });
//添加以第一列的数据
model.addRow(new Object[] { "v1", "v2", "v3" });
//设置布局
JFrame f = new JFrame();
f.setSize(300, 300);
f.add(new JScrollPane(table));
f.setVisible(true);
}
}