JAVA事件监听机制学习

//事件监听机制 
  
 import java.awt.*; 
 import java.awt.event.*; 
  
 public class TestEvent { 
     public static void main(String[] args) { 
         Frame f = new Frame("Test"); 
         Button b = new Button("Press Me!"); 
  
         Monitor bh = new Monitor(); //实现了某种监听器接口的类的对象 
  
         b.addActionListener(bh);    //注册 
  
         f.add(b, BorderLayout.CENTER); 
         f.pack(); 
         f.setVisible(true); 
     } 
 } 
  
 //实现了某种监听器接口的类 
class Monitor implements ActionListener { 
     public void actionPerformed(ActionEvent e) { 
         System.out.println("A button has been pressed"); 
     } 
 } 
 [java]
 import java.awt.*; 
 import java.awt.event.*; 
  
public class TestActionEvent { 
     public static void main(String[] args) { 
         Frame f = new Frame("Test"); 
         Button b1 = new Button("Start"); 
         Button b2 = new Button("Stop"); 
  
         //一个监听器类被两个button监听 
         Monitor2 bh = new Monitor2(); 
         b1.addActionListener(bh); 
         b2.addActionListener(bh); 
  
         b2.setActionCommand("Game over"); 
  
         f.add(b1,"North"); 
         f.add(b2,"Center"); 
         f.pack(); 
  
         f.setVisible(true); 
  
     } 
  
 } 
  
 class Monitor2 implements ActionListener { 
     public void actionPerformed (ActionEvent e) { 
         System.out.println("A button has been pressed," + 
             "the relative info is:
" + e.getActionCommand()); 
     } 
 } 

 

引自http://www.2cto.com/kf/201205/133664.html

http://ericliu1986.iteye.com/blog/629562

java事件机制包括三个部分:事件、事件监听器、事件源。

1、事件。一般继承自java.util.EventObject类,封装了事件源对象及跟事件相关的信息。

com.javaedu.event.CusEvent类

package com.javaedu.event;

import java.util.EventObject;

/**
 * 事件类,用于封装事件源及一些与事件相关的参数.
 * @author Eric
 */
public class CusEvent extends EventObject {
    private static final long serialVersionUID = 1L;
    private Object source;//事件源
    
    public CusEvent(Object source){
        super(source);
        this.source = source;
    }

    public Object getSource() {
        return source;
    }

    public void setSource(Object source) {
        this.source = source;
    }
}

2、事件监听器。实现java.util.EventListener接口,注册在事件源上,当事件源的属性或状态改变时,取得相应的监听器调用其内部的回调方法。

com.javaedu.event.CusEventListener类

package com.javaedu.event;

import java.util.EventListener;

/**
 * 事件监听器,实现java.util.EventListener接口。定义回调方法,将你想要做的事
 * 放到这个方法下,因为事件源发生相应的事件时会调用这个方法。
 * @author Eric
 */
public class CusEventListener implements EventListener {
    
    //事件发生后的回调方法
    public void fireCusEvent(CusEvent e){
        EventSourceObjecteObject = (EventSourceObject)e.getSource();
        System.out.println("My name has been changed!");
        System.out.println("I got a new name,named ""+eObject.getName()+""");    }
}

3、事件源。事件发生的地方,由于事件源的某项属性或状态发生了改变(比如BUTTON被单击、TEXTBOX的值发生改变等等)导致某项事件发生。换句话说就是生成了相应的事件对象。因为事件监听器要注册在事件源上,所以事件源类中应该要有盛装监听器的容器(List,Set等等)。

 com.javaedu.event.EventSourceObject类

package com.javaedu.event;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * 事件源.
 * @author Eric
 */
public class EventSourceObject {
    private String name;
    //监听器容器
    private Set<CusEventListener> listener;
    public EventSourceObject(){
        this.listener = new HashSet<CusEventListener>();
        this.name = "defaultname";
    }
    //给事件源注册监听器
    public void addCusListener(CusEventListener cel){
        this.listener.add(cel);
    }
    //当事件发生时,通知注册在该事件源上的所有监听器做出相应的反应(调用回调方法)
    protected void notifies(){
        CusEventListener cel = null;
        Iterator<CusEventListener> iterator = this.listener.iterator();
        while(iterator.hasNext()){
            cel = iterator.next();
            cel.fireCusEvent(new CusEvent(this));
        }
    }
    public String getName() {
        return name;
    }
    //模拟事件触发器,当成员变量name的值发生变化时,触发事件。
    public void setName(String name) {
        if(!this.name.equals(name)){
            this.name = name;
            notifies();
        }      
    }
}

下面是主方法类

 com.javaedu.event.MainTest类

package com.javaedu.event;

public class MainTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        EventSourceObject object = new EventSourceObject();
        //注册监听器
        object.addCusListener(new CusEventListener(){
            @Override
            public void fireCusEvent(CusEvent e) {
                super.fireCusEvent(e);
            }
        });
        //触发事件
        object.setName("eric");
    }
}
原文地址:https://www.cnblogs.com/Anita9002/p/3953180.html