一个事件源一个事件类只能有一个事件监听器吗?还是可以有多个事件监听器,并且事件发生时依次执行各个监听器中的事件处理代码?
比如说,
button1.addMouseListener(l1);
button1.addMouseListener(l2);
如果只能有一个事件监听器,这时button1的MouseListener是哪个?

解决方案 »

  1.   

    两个事件同时被相应,刚才写的一个例子,可以说明这个问题
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JApplet;
    import javax.swing.JButton;public class ButtonOpenWebpage extends JApplet {
    JButton jb = new JButton("打开我要的地址!"); ActionListener al = new ActionListener() { public void actionPerformed(ActionEvent e) {
    try {
    Runtime
    .getRuntime()
    .exec(
    "C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE http://www.csdn.net");
    } catch (Exception ef) {
    ef.printStackTrace();
    } }
    };

    ActionListener a2 = new ActionListener() { public void actionPerformed(ActionEvent e) {
    try {
    Runtime
    .getRuntime()
    .exec(
    "C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE http://www.sina.com.cn");
    } catch (Exception ef) {
    ef.printStackTrace();
    } }
    }; public void init() {
    Container cp = getContentPane();
    cp.setLayout(new FlowLayout());
    jb.addActionListener(al);
    jb.addActionListener(a2);
    cp.add(jb);
    }
    }
      

  2.   

    不过响应的顺序是按照
    jb.addActionListener(al);
    jb.addActionListener(a2);
    这个前后顺序