Object event.getSource()可以判断是从哪里来得 event

解决方案 »

  1.   

    同意楼上~
    a = event.getSource();
    if(a instanceof Button)
    ...
      

  2.   

    String getActionCommand()也可以。不过要先用 setActionCommand()设置好。
      

  3.   

    你的actionlistener中的方法比如
    public void acitonPerformed(ActionEvent e){
        if(e.getActionCommand ==""){
        }
    }
    通过ActionEvent来获得事件源
    下面是一个例子
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    public class Frame1 extends JFrame implements ActionListener {
      JButton jButton2 = new JButton();
      JButton jButton3 = new JButton();
      public Frame1() {
        try {
          jbInit();
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      public static void main(String[] args) {
        Frame1 frame1 = new Frame1();
        frame1.setSize(800,600);
        frame1.setVisible(true);
      }
      private void jbInit() throws Exception {
        this.getContentPane().setLayout(null);
        jButton2.addActionListener(this);
        jButton2.setText("jButton2");
        jButton2.setBounds(new Rectangle(177, 135, 56, 21));
        jButton3.setText("jButton3");
        jButton3.addActionListener(this);
        jButton3.setBounds(new Rectangle(257, 140, 54, 23));
        this.getContentPane().add(jButton2, null);
        this.getContentPane().add(jButton3, null);
      }  public void actionPerformed(ActionEvent e){
          System.out.println(e.getSource());
          System.out.println(e.getActionCommand());
      }
    }