日期:2014-05-20  浏览次数:20735 次

帮小妹看看这个是什么问题?或你有更好方法解决(谁的方法最好,我会把100分直接送你))
Java code

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;

public class Test extends JFrame {
    

    private JTable table;
    private DefaultTableModel cardModel;
    /**
     * Launch the application
     * @param args
     */
    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Test frame = new Test();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame
     */
    public Test() {
        super();
        setBounds(100, 100, 500, 375);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JScrollPane scrollPane = new JScrollPane();
        getContentPane().add(scrollPane, BorderLayout.CENTER);

        table = new JTable();
        scrollPane.setViewportView(table);
        //
        initTable();
        tableVal();
        table.addMouseListener(new MouseSelect());
    }
     private void initTable(){
           cardModel = new DefaultTableModel(6,3);
           table.setModel(cardModel);
           table.getColumnModel().getColumn(0).setHeaderValue("1");
           table.getColumnModel().getColumn(1).setHeaderValue("2");
           table.getColumnModel().getColumn(2).setHeaderValue("3");
           table.setRowHeight(25);
       }
      public void tableVal() {
          String[] content={"性别","星期"};
          JComboBox contentType = new JComboBox();
            for(int i=0;i<content.length;i++){
                contentType.addItem(content[i]);
            }
            DefaultCellEditor contentcell = new DefaultCellEditor(contentType);
            TableColumnModel tcm = table.getColumnModel();
            tcm.getColumn(0).setCellEditor(contentcell);
                
        }
      private class MouseSelect implements MouseListener {

        @Override
        public void mouseClicked(MouseEvent e) {
            // TODO Auto-generated method stub
            Object target = e.getSource();
             if (target instanceof JTable) {
                 int row=table.getSelectedRow();
                    int col=table.getSelectedColumn();
                    String name=table.getValueAt(row, col).toString();
                    if(col==0){
                        if(name.equals("性别")){
                            String[] data={"男","女"};
                            JComboBox dataBox = new JComboBox();
                            for(int i=0;i<data.length;i++){
                                dataBox.addItem(data[i]);
                            }
                            DefaultCellEditor dataCell = new DefaultCellEditor(dataBox);
                            TableColumnModel dataTcm = table.getColumnModel();
                            dataTcm.getColumn(1).setCellEditor(dataCell);
                        }
                        if(name.equals("星期")){
                             String[] date={"星期1","星期2","星期3","星期4"};
                            JComboBox dateBox = new JComboBox();
                            for(int i=0;i<date.length;i++){
                                dateBox.addItem(date[i]);
                            }
                            DefaultCellEditor dateCell = new DefaultCellEditor(dateBox);
                            TableColumnModel dateTcm = table.getColumnModel();
                            dateTcm.getColumn(1).setCellEditor(dateCell);
                        }
                    }
             }
            
            
        }

        @Override
        public void mouseEntered(MouseEvent arg0) {
            // TODO Auto-generated method stub
            
        }

        @Override
        public void mouseExited(MouseEvent arg0) {
            // TODO Auto-generated method stub
            
        }

        @Override
        public void mousePressed(MouseEvent arg0) {
            // TODO Auto-generated method stub
            
        }

        @Override
        public void mouseReleased(MouseEvent arg0) {
            // TODO Auto-generated method stub
            
        }
          
      }
    
}