public class StatisticFrame extends JFrame{
public StatisticFrame(){
init();
}
private JTable table;
private void init() {
this.setTitle("statistic");
this.setSize(600, 300);
JToolBar toolBar = new JToolBar(); 
JButton button1 = new JButton("应用层");
JButton button2 = new JButton("网络层");
JButton button3 = new JButton("数据链路层");
toolBar.add(button1);
toolBar.add(button2);
toolBar.add(button3);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Vector head = new Vector();
head.add("端口");
head.add("协议");
head.add("位置");
DefaultTableModel model = new DefaultTableModel(null,head);
table= new JTable(model);
JScrollPane pane = new JScrollPane(table);
add(pane);
}
});
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Vector head = new Vector();
head.add("协议");
head.add("位置");
DefaultTableModel model = new DefaultTableModel(null,head);
table= new JTable(model);
JScrollPane pane = new JScrollPane(table);
add(pane);
}
});
this.add(BorderLayout.NORTH,toolBar);
} public static void main(String[] args) {
StatisticFrame frame = new StatisticFrame();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
当我点击JButton1时如何生成一个Jtable在主窗口区,求教!Jbutton,Jtable

解决方案 »

  1.   

    我只给你改了一个按钮的你自己看看吧
    import javax.swing.*;
    import javax.swing.table.DefaultTableModel;public class StatisticFrame extends JFrame implements ActionListener {
    public StatisticFrame() {
    init();
    } private JTable table;
    Vector head = new Vector();
    JButton button1; private void init() {
    this.setTitle("statistic");
    this.setSize(600, 300);
    DefaultTableModel model = new DefaultTableModel(null, head);
    table = new JTable(model);
    JScrollPane pane = new JScrollPane(table);
    JToolBar toolBar = new JToolBar();
    button1 = new JButton("应用层");
    JButton button2 = new JButton("网络层");
    JButton button3 = new JButton("数据链路层");
    toolBar.add(button1);
    toolBar.add(button2);
    toolBar.add(button3);
    this.add(pane);
    button1.addActionListener(this);
    this.add(BorderLayout.NORTH, toolBar);
    } public static void main(String[] args) {
    StatisticFrame frame = new StatisticFrame();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    } @Override
    public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    if (e.getSource() == button1) {
    head.add("端口");
    head.add("协议");
    head.add("位置");
    DefaultTableModel model = new DefaultTableModel(null, head);
    table.setModel(model);
    }
    }
    }