分析源码,探究AWT事件处理机制

 

Java中的事件处理机制,实际是一种委托模型。比如按钮,当我们发出点击按钮的动作之后,真正响应事件的业务逻辑并非按钮本身,而是由按钮委托的相应监听器来完成的。

在探究之前,必须先了解事件的三个重要概念:

1)事件(event)

比如,点击按钮,这一动作就会产生一个事件,这个事件包含一些信息,如在什么时候点击,事件源的一些信息(比如按钮上的标签)等等,事件类就是对这些信息的一个封装,比如ActionEvent类;

这些类的继承关系如下:


注意:只要是事件,都是类,而不是接口,因为它是对事件信息的一个封装;

(2) 事件源(event source) 
   就是发生事件的组件,比如说按钮Button就是一个事件源,他可以发生ActionEvent这样的事件。这些事件源类型的父类就是Component。

(3) 事件监听器(event listener) 
    这是一些相应事件,并完成相应逻辑功能的对象。
    比如点击按钮,产生了一个ActionEvent对象,但需要完成相应的业务逻辑。必须使用到ActionListener监听器。但要注意ActionListener并不是类,而是接口,不同的业务逻辑要封装在完成ActionListener接口的不同类中。
    这些监听器接口都继承了接口java.util EventListener 。

注意:事件监听器是接口,因为对于特定的事件,需要特定的实现;

Listener是有一个方法:actionPerformed();


总结:awt事件机制的过程是:

1)。 由事件源(Button)产生一个与动作(按下按钮并释放)相适应的ActionEvent事件。
(2) 事件源会将这个ActionEvent事件发送给监听器,并执行监听器的actionPerformed(e)方法来完成事件响应。

但是事件源并不知道由哪一个监听器来完成,所以重点是探究如何为事件源添加监听器

源码解释最权威:(只选取类中能说明为题的方法)

 

首先事件源Button

在button类中维护的是一个个监听器Listener

*/

public classButton extends Component implementsAccessible {

{

transientActionListener actionListener;;

//AWTEvent类既是本地实现的事件(比如单击按钮)

//该方法判断是否有事件发生,并把事件作为参数传入

boolean eventEnabled(AWTEvent e) {

        if (e.id == ActionEvent.ACTION_PERFORMED) {

            if ((eventMask & AWTEvent.ACTION_EVENT_MASK) != 0 ||

                actionListener != null) {

                return true;

            }

            return false;

        }

        return super.eventEnabled(e);

}

    //通过该中间方法处理事件

protected void processEvent(AWTEvent e) {

        if (e instanceof ActionEvent) {

            processActionEvent((ActionEvent)e);

            return;

        }

    super.processEvent(e);

    }

//此方法才是真正执行监听器中的方法的方法;

protected void processActionEvent(ActionEvente) {

        ActionListener listener = actionListener;

        if (listener != null) {

            listener.actionPerformed(e);

        }

    }

//通过此方法添加监听器,但真正添加的算法在AWTEventMulticaster中实现

public synchronized void addActionListener(ActionListener l) {

    if (l == null) {

        return;

    }

    actionListener = AWTEventMulticaster.add(actionListener, l);

public class AWTEventMulticasterimplements

    ComponentListener, ContainerListener,FocusListener, KeyListener,

    MouseListener, MouseMotionListener,WindowListener, WindowFocusListener,

    WindowStateListener, ActionListener,ItemListener, AdjustmentListener,

    TextListener, InputMethodListener,HierarchyListener,

    HierarchyBoundsListener, MouseWheelListener{

 

protected finalEventListener a, b;

protected AWTEventMulticaster(EventListenera, EventListener b) {

    this.a = a; this.b = b;

    }

//返回EventListener类型

protected static EventListeneraddInternal(EventListener a, EventListener b) {

    if (a == nullreturn b;

    if (b == nullreturn a;

    return newAWTEventMulticaster(a, b);

    }

 

  public static ActionListener add(ActionListener a, ActionListener b) {

        return (ActionListener)addInternal(a,b);

 }

以上就是AWT事件机制的实现机制(从一个小的角度观察);

 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值