当前位置: 代码迷 >> J2EE >> 关于JTable的使用解决思路
  详细解决方案

关于JTable的使用解决思路

热度:86   发布时间:2016-04-22 01:28:55.0
关于JTable的使用
Java code
package view;import java.awt.BorderLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.Vector;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.JTextField;import javax.swing.table.DefaultTableModel;import dao.TransinfoDAO;public class MainFrame extends JFrame implements ActionListener {    JPanel topPane = new JPanel();    JLabel l_sel = new JLabel("请输入查询内容:");    JTextField tf_sel = new JTextField(10);    JButton b_sel = new JButton("查询");        TransinfoDAO td = new TransinfoDAO();    Vector cols = getCols();    Vector rows = td.getAllTransinfo();    DefaultTableModel model = new DefaultTableModel(rows,cols);    JTable table = new JTable(model);    JScrollPane centerPane = new JScrollPane(table);        JPanel bottomPane = new JPanel();    JButton b_add = new JButton("添加消费信息");    JButton b_del = new JButton("删除消费信息");    JButton b_edit = new JButton("编辑消费信息");    JButton b_exit = new JButton("退出系统");    public MainFrame(){        topPane.add(this.l_sel);        topPane.add(this.tf_sel);        topPane.add(this.b_sel);                bottomPane.add(this.b_add);        bottomPane.add(this.b_edit);        bottomPane.add(this.b_del);        bottomPane.add(this.b_exit);                b_sel.addActionListener(this);        b_add.addActionListener(this);        b_edit.addActionListener(this);        b_del.addActionListener(this);        b_exit.addActionListener(this);                this.add(this.topPane,BorderLayout.NORTH);        this.add(this.centerPane,BorderLayout.CENTER);        this.add(this.bottomPane,BorderLayout.SOUTH);                this.setSize(600, 400);        this.setLocationRelativeTo(null);        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        this.setVisible(true);    }        public void actionPerformed(ActionEvent e) {        if(e.getSource()==this.b_add){            TransinfoAddFrame taf = new TransinfoAddFrame(this);        }    }    public void setTableModel(){        Vector cols = getCols();        Vector rows = td.getAllTransinfo();        DefaultTableModel model = new DefaultTableModel(rows,cols);        table.setModel(model);    }        public Vector getCols(){        Vector cols = new Vector();        cols.add("消费id");        cols.add("成员");        cols.add("项目");        cols.add("类型");        cols.add("金额");        cols.add("时间");        cols.add("支付方式");        return cols;    }    public static void main(String[] args) {        MainFrame mf = new MainFrame();    }}


哪位大虾可以帮小弟看下,为什么这里的集合要定义两次.我试着把cols与rows定位全局变量,结果表格就出不来了.
Java code
        TransinfoDAO td = new TransinfoDAO();    Vector cols = getCols();    Vector rows = td.getAllTransinfo();    DefaultTableModel model = new DefaultTableModel(rows,cols);    JTable table = new JTable(model);    JScrollPane centerPane = new JScrollPane(table);

Java code
    public void setTableModel(){        Vector cols = getCols();        Vector rows = td.getAllTransinfo();        DefaultTableModel model = new DefaultTableModel(rows,cols);        table.setModel(model);    }



------解决方案--------------------
对于JTable了解的不多 
优酷上有视频搜搜可以找到
  相关解决方案