public class FrameTable extends JFrame {
    XYLayout xYLayout1 = new XYLayout();
    JScrollPane jScrollPane1 = new JScrollPane();
    //创建表格的模型
    String colhead[] = {"学号", "姓名", "性别", "出生日期", "籍贯"};
    //表示初始化2条空记录
    DefaultTableModel dtm = new DefaultTableModel(colhead, 2);
    JTable jTable1 = new JTable();
    JLabel jLabel1 = new JLabel();
    JTextField jTextField1 = new JTextField();
    JLabel jLabel2 = new JLabel();
    JTextField jTextField2 = new JTextField();
    JLabel jLabel3 = new JLabel();
    ButtonGroup buttonGroup1 = new ButtonGroup();
    JRadioButton jRadioButton1 = new JRadioButton();
    JRadioButton jRadioButton2 = new JRadioButton();
    JLabel jLabel4 = new JLabel();
    JTextField jTextField3 = new JTextField();
    JComboBox jComboBox1 = new JComboBox();
    JLabel jLabel5 = new JLabel();
    JButton jButton1 = new JButton();
    JButton jButton2 = new JButton();    private void jbInit(){
        //把表模型和表关联
        jTable1.setModel(dtm);
        getContentPane().setLayout(xYLayout1);
        jLabel1.setText("学号");
        jLabel2.setText("姓名");
        jLabel3.setText("性别");
        jRadioButton1.setSelected(true);
        jRadioButton1.setText("男");
        jRadioButton2.setText("女");
        jLabel4.setText("出生日期");
        jLabel5.setText("籍贯");
        jButton1.setText("添加");
        jButton1.addActionListener(new FrameTest_jButton1_actionAdapter(this));
        jComboBox1.addItem("广东省");
        jComboBox1.addItem("云南省");
        jComboBox1.addItem("贵州省");
        jComboBox1.addItem("四川省");
        jComboBox1.addItem("湖南省");
        jComboBox1.addItem("湖北省");
        jComboBox1.addItem("江西省");
        jComboBox1.addItem("江苏省");
        jComboBox1.addItem("山东省");
        jButton2.setText("删 除");
        jButton2.addActionListener(new FrameTable_jButton2_actionAdapter(this));
        this.getContentPane().add(jScrollPane1,
                                  new XYConstraints(7, 98, 379, 192));
        this.getContentPane().add(jLabel3, new XYConstraints(6, 34, 39, 21));
        this.getContentPane().add(jRadioButton1,
                                  new XYConstraints(40, 32, -1, -1));
        this.getContentPane().add(jLabel4, new XYConstraints(156, 33, 52, 21));
        this.getContentPane().add(jTextField3,
                                  new XYConstraints(220, 34, 112, 24));
        this.getContentPane().add(jComboBox1, new XYConstraints(44, 64, 105, 22));
        this.getContentPane().add(jLabel5, new XYConstraints(6, 66, 35, 20)); //把表添加到滚动面板
        jScrollPane1.getViewport().add(jTable1);
        buttonGroup1.add(jRadioButton1);
        buttonGroup1.add(jRadioButton2);
        this.getContentPane().add(jTextField1, new XYConstraints(44, 4, 90, 24));        this.getContentPane().add(jTextField2,
                                  new XYConstraints(220, 4, 112, 22));
        this.getContentPane().add(jLabel2, new XYConstraints(155, 5, 54, 19));
        this.getContentPane().add(jLabel1, new XYConstraints(5, 4, 62, 22));
        this.getContentPane().add(jRadioButton2,
                                  new XYConstraints(87, 35, 40, 20));
        this.getContentPane().add(jButton1, new XYConstraints(155, 67, 81, 24));
        this.getContentPane().add(jButton2, new XYConstraints(257, 67, 75, 24));
        this.setSize(400,300);
        this.setVisible(true);
    }    public void jButton1_actionPerformed(ActionEvent e) {
        String id = jTextField1.getText().trim();
        String name = jTextField2.getText().trim();
        String sex = null;
        if (jRadioButton1.isSelected()) {
            sex = "男";
        } else {
            sex = "女";
        }
        String birth = jTextField3.getText().trim();
        String prov = jComboBox1.getSelectedItem().toString().trim();
        String val[] = {id, name, sex, birth, prov};
        dtm.addRow(val);
    }    public void jButton2_actionPerformed(ActionEvent e) {
        int i = dtm.getRowCount();
        if (i > 0) {
            dtm.removeRow(i);
        }
    }
}
class FrameTable_jButton2_actionAdapter implements ActionListener {
    private FrameTable adaptee;
    FrameTable_jButton2_actionAdapter(FrameTable adaptee) {
        this.adaptee = adaptee;
    }    public void actionPerformed(ActionEvent e) {
        adaptee.jButton2_actionPerformed(e);
    }
}
class FrameTest_jButton1_actionAdapter implements ActionListener {
    private FrameTable adaptee;
    FrameTest_jButton1_actionAdapter(FrameTable adaptee) {
        this.adaptee = adaptee;
    }    public void actionPerformed(ActionEvent e) {
        adaptee.jButton1_actionPerformed(e);
    }
}
有没有人帮我解决下??????????

解决方案 »

  1.   

    public class FrameTable extends JFrame { 
        XYLayout xYLayout1 = new XYLayout(); 
        JScrollPane jScrollPane1 = new JScrollPane(); 
        //创建表格的模型 
        String colhead[] = {"学号", "姓名", "性别", "出生日期", "籍贯"}; 
        //表示初始化2条空记录 
        DefaultTableModel dtm = new DefaultTableModel(colhead, 2); 
        JTable jTable1 = new JTable(); 
        JLabel jLabel1 = new JLabel(); 
        JTextField jTextField1 = new JTextField(); 
        JLabel jLabel2 = new JLabel(); 
        JTextField jTextField2 = new JTextField(); 
        JLabel jLabel3 = new JLabel(); 
        ButtonGroup buttonGroup1 = new ButtonGroup(); 
        JRadioButton jRadioButton1 = new JRadioButton(); 
        JRadioButton jRadioButton2 = new JRadioButton(); 
        JLabel jLabel4 = new JLabel(); 
        JTextField jTextField3 = new JTextField(); 
        JComboBox jComboBox1 = new JComboBox(); 
        JLabel jLabel5 = new JLabel(); 
        JButton jButton1 = new JButton(); 
        JButton jButton2 = new JButton();     private void jbInit(){ 
            //把表模型和表关联 
            jTable1.setModel(dtm); 
            getContentPane().setLayout(xYLayout1); 
            jLabel1.setText("学号"); 
            jLabel2.setText("姓名"); 
            jLabel3.setText("性别"); 
            jRadioButton1.setSelected(true); 
            jRadioButton1.setText("男"); 
            jRadioButton2.setText("女"); 
            jLabel4.setText("出生日期"); 
            jLabel5.setText("籍贯"); 
            jButton1.setText("添加"); 
            jButton1.addActionListener(new FrameTest_jButton1_actionAdapter(this)); 
            jComboBox1.addItem("广东省"); 
            jComboBox1.addItem("云南省"); 
            jComboBox1.addItem("贵州省"); 
            jComboBox1.addItem("四川省"); 
            jComboBox1.addItem("湖南省"); 
            jComboBox1.addItem("湖北省"); 
            jComboBox1.addItem("江西省"); 
            jComboBox1.addItem("江苏省"); 
            jComboBox1.addItem("山东省"); 
            jButton2.setText("删 除"); 
            jButton2.addActionListener(new FrameTable_jButton2_actionAdapter(this)); 
            this.getContentPane().add(jScrollPane1, 
                                      new XYConstraints(7, 98, 379, 192)); 
            this.getContentPane().add(jLabel3, new XYConstraints(6, 34, 39, 21)); 
            this.getContentPane().add(jRadioButton1, 
                                      new XYConstraints(40, 32, -1, -1)); 
            this.getContentPane().add(jLabel4, new XYConstraints(156, 33, 52, 21)); 
            this.getContentPane().add(jTextField3, 
                                      new XYConstraints(220, 34, 112, 24)); 
            this.getContentPane().add(jComboBox1, new XYConstraints(44, 64, 105, 22)); 
            this.getContentPane().add(jLabel5, new XYConstraints(6, 66, 35, 20)); //把表添加到滚动面板 
            jScrollPane1.getViewport().add(jTable1); 
            buttonGroup1.add(jRadioButton1); 
            buttonGroup1.add(jRadioButton2); 
            this.getContentPane().add(jTextField1, new XYConstraints(44, 4, 90, 24));         this.getContentPane().add(jTextField2, 
                                      new XYConstraints(220, 4, 112, 22)); 
            this.getContentPane().add(jLabel2, new XYConstraints(155, 5, 54, 19)); 
            this.getContentPane().add(jLabel1, new XYConstraints(5, 4, 62, 22)); 
            this.getContentPane().add(jRadioButton2, 
                                      new XYConstraints(87, 35, 40, 20)); 
            this.getContentPane().add(jButton1, new XYConstraints(155, 67, 81, 24)); 
            this.getContentPane().add(jButton2, new XYConstraints(257, 67, 75, 24)); 
            this.setSize(400,300); 
            this.setVisible(true); 
        }     public void jButton1_actionPerformed(ActionEvent e) { 
            String id = jTextField1.getText().trim(); 
            String name = jTextField2.getText().trim(); 
            String sex = null; 
            if (jRadioButton1.isSelected()) { 
                sex = "男"; 
            } else { 
                sex = "女"; 
            } 
            String birth = jTextField3.getText().trim(); 
            String prov = jComboBox1.getSelectedItem().toString().trim(); 
            String val[] = {id, name, sex, birth, prov}; 
            dtm.addRow(val); 
        }     public void jButton2_actionPerformed(ActionEvent e) { 
            int i = dtm.getRowCount(); 
            if (i > 0) { 
                dtm.removeRow(i-1); 
            } 
        } 

    class FrameTable_jButton2_actionAdapter implements ActionListener { 
        private FrameTable adaptee; 
        FrameTable_jButton2_actionAdapter(FrameTable adaptee) { 
            this.adaptee = adaptee; 
        }     public void actionPerformed(ActionEvent e) { 
            adaptee.jButton2_actionPerformed(e); 
        } 

    class FrameTest_jButton1_actionAdapter implements ActionListener { 
        private FrameTable adaptee; 
        FrameTest_jButton1_actionAdapter(FrameTable adaptee) { 
            this.adaptee = adaptee; 
        }     public void actionPerformed(ActionEvent e) { 
            adaptee.jButton1_actionPerformed(e); 
        } 
    } 可以了