我想监听很多个按钮,点击一次后,把点击的那个按钮setEnabled(false)
该怎样做啊

解决方案 »

  1.   

    public void actionPerformed(ActionEvent e) {
    for(int i=0;i<W;i++){
    for(int j=0;j<H;j++){
    if(e.getSource()==jb[i][j]){
    jb[i][j].setEnabled(false);
    }
    }
    }
    总觉得怪怪的。
      

  2.   

    if(e.getSource()==点击的按钮){
                        点击的按钮.setEnabled(false);
                    }
      

  3.   

    所以才说你用循环做是对的啊public class DragFileTest extends JFrame implements ActionListener{
    private static final long serialVersionUID = 1L;
    JButton[][] jb;

    public DragFileTest() {
    super("frame");
    this.setLayout(new GridLayout(3,3));
    jb = new JButton[3][3];
    for(int i=0;i<3;i++){
    for(int j=0;j<3;j++){
    jb[i][j] = new JButton("b"+i+" "+j);
    jb[i][j].addActionListener(this);
    this.getContentPane().add(jb[i][j]);
    }
    }
    } public static void main(String[] args) {
    DragFileTest ts = new DragFileTest();
    ts.setBounds(200, 200, 300, 200);
    ts.setVisible(true);
    } public void actionPerformed(ActionEvent e) {
    for(int i=0;i<3;i++){
                for(int j=0;j<3;j++){
                    if(e.getSource()==jb[i][j]){
                        jb[i][j].setEnabled(false);
                    }
                }
    }
    }
    }
      

  4.   

    e.getSource()就是当前点击的按钮!        button.addMouseListener(new MouseAdapter() {            @Override
                public void mouseClicked(MouseEvent e) {
                    if (e.getSource() instanceof JButton) {
                        JButton thisIsCurrentButton = (JButton) e.getSource();
                        thisIsCurrentButton.setEnabled(false);
                        JOptionPane.showMessageDialog(null, thisIsCurrentButton.getText());
                    }
                }        });
      

  5.   

    =============================================================
    我也想过把它(JButton)强转下。
    =============================================================