JTable 实例
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout; import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel; public class main {
static JPanel middlePanel; static void paint() {
middlePanel = new JPanel();
String[] columnName = { "股票名字", "今日开盘价", "昨日收盘价", "当前价格", "今日最高价","今日最低价", "竞买价",
"竞卖价", "成交的股票数", "成交金额", "买一申请", "买一报价", "买二申请", "买二报价", "买三申请",
"买三报价","买四申请", "买四报价", "买五申请","买五报价","卖一申请", "卖一报价", "卖二申请", "卖二报价",
"卖三申请", "卖三报价","卖四申请", "卖四报价","卖五申请", "卖五报价", "日期", "时间"};
String[][] rowData = { {"大秦铁路","27.55","27.25","26.91", "27.55", "26.20"," 26.91", "26.92","22114263"
,"589824680","4695","26.91","57590","26.90","14700","26.89","14300","26.88","15100","26.87","3100","26.92","8900","26.93",
"14230","26.94","25150","26.95","15220","26.96","2008-01-11","15:05:32"},
}; // 创建表格
JTable table = new JTable(new DefaultTableModel(rowData, columnName));
// 创建包含表格的滚动窗格
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
// 定义 topPanel 的布局为 BoxLayout,BoxLayout 为垂直排列
middlePanel.setLayout(new BoxLayout(middlePanel, BoxLayout.Y_AXIS));
// 先加入一个不可见的 Strut,从而使 topPanel 对顶部留出一定的空间
middlePanel.add(Box.createVerticalStrut(10));
// 加入包含表格的滚动窗格
middlePanel.add(scrollPane);
} public static void main(String[] args) {
paint(); JPanel panelContainer = new JPanel();
panelContainer.setLayout(new GridBagLayout()); GridBagConstraints c2 = new GridBagConstraints();
c2.gridx = 0;
c2.gridy = 10;
c2.weightx = 0.4;
c2.weighty = 0;
c2.fill = GridBagConstraints.HORIZONTAL ;
// 加入 middlePanel
panelContainer.add(middlePanel,c2); JFrame frame = new JFrame("Table 演示");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
panelContainer.setOpaque(true);
frame.setSize(new Dimension(2000, 600));
frame.setContentPane(panelContainer);
frame.setVisible(true);
} }