日期:2014-05-20 浏览次数:21029 次
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.awt.*;
import java.awt.event.*;
public class Test implements ActionListener{
    JTable table=null;
    MyTable mt=null;
    JTextArea ta=null;
    JFrame f=null;
    Container contentPane=null;
    JButton b1=null;
    JButton b2=null;
    public Test(){
        f=new JFrame();
        contentPane = f.getContentPane();
        contentPane.setLayout(new CardLayout());
        mt=new MyTable();
        table = new JTable(mt);
        table.setPreferredScrollableViewportSize(new Dimension(550,90));
        JScrollPane s=new JScrollPane(table);
        JPanel p1=new JPanel();
        p1.setLayout(new GridLayout(2,1));
        JPanel p11=new JPanel();
        p1.add(s);
        b1=new JButton("提交");
        b1.addActionListener(this);
        p11.add(b1,BorderLayout.CENTER);
        p1.add(p11);
        JPanel p2=new JPanel();
        ta=new JTextArea("已选"+"\n");
        ta.setSize(550, 30);
        p2.setLayout(new GridLayout(2,1));
        p2.add(ta);
        JPanel p21=new JPanel();
        b2=new JButton("确定");
        b2.addActionListener(this);
        p21.add(b2,BorderLayout.CENTER);
        p2.add(p21);
        contentPane.add("one",p1);contentPane.add("two",p2);
        ((CardLayout)contentPane.getLayout()).show(contentPane, "one");
        f.setTitle("bixiu");
        f.pack();
        f.setVisible(true);
        f.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });
    }
    @Override
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==b1){//提交按钮
            for(int i=0;i<table.getRowCount();i++){   //这里修改了
                //boolean choice=(Boolean) mt.getValueAt(i, 3);
                boolean choice=Boolean.parseBoolean(""+mt.getValueAt(i,2));
                if(choice){
                    //下面这行修改了
                    ta.append("课程: "+mt.getValueAt(i, 0)+"\t"+"学分: "+mt.getValueAt(i, 1));
                }
            }
            ((CardLayout)contentPane.getLayout()).show(contentPane,"two");
        }
        if(e.getSource()==b2){
        }
        table.revalidate();
        f.validate();
    }
    public static void main(String args[]){
        new Test();
    }
}
class MyTable extends AbstractTableModel{
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    Object[][]p={
            {"math",new Integer(6),new Boolean(false)},
            {"C++",new Integer(5),new Boolean(false)},
            {"Chinese",new Integer(3),new Boolean(false)},
            {"English",new Integer(4),new Boolean(false)},
    };
    String[]n={"Name","XueFen","Choose"};
    Class columnsClass[]={String.class,Integer.class,Boolean.class};
    public int getColumnCount(){
        return n.length;
    }
    public int getRowCount(){
        return p.length;
    }
    public String getColumnName(int col){
        return n[col];
    }
    public Object getValueAt(int row,int col){
        return p[row][col];
    }
    public Class<?> getColumnClass(int c){
        return columnsClass[c];
    }
    public boolean isCellEditable(int rowIndex,int columnIndex){
        return true;
    }
    public void setValueAt(Object value,int row,int col){
        p[row][col]=value;
        fireTableCellUpdated(row,col);
    }
}