事件模型学习

来源:互联网 发布:php接口怎么写 编辑:程序博客网 时间:2024/05/21 10:07
<script type="text/javascript">google_ad_client = "pub-8800625213955058";/* 336x280, 创建于 07-11-21 */google_ad_slot = "0989131976";google_ad_width = 336;google_ad_height = 280;//</script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>定制自己的事件模型的几个步骤:一、基本知识:事件响应机制模型图 二、自定义事件1、必须extends AWTEvent,因为AWT事件队列中的所有事件必须为AWTEvent。2、必须提供事件源对象作为AWTEvent构造器的参数。3、必须提供该事件的ID号。一般取一个系统保留之外的任意整数。4、当然,也可以在事件中增加其他的数据域或者方法。class myevent extends AWTEvent{public static final int my_event=AWTEvent.RESERVED_ID_MAX 90;public myevent (my t) //当然可以在此增加关于事件源的其他的信息以便增加特定事件信息//的完整性。{super(t,my_event);}}三、自定义监听类型1、自定义某种监听接口,必须extends EventListener,增加特定的方法。2、方法的参数必须为自定义的事件。interface mylistener extends EventListener{void dosomething (myevent event);}四、在事件源中管理这种自定义的监听和事件。1、首先是在事件源的对象中能够添加某种监听器类型的机制。public void addmylistener(mylistener listener){listenerList.add(mylistener.class,listener);}public void removemylistener(mylistener listener){listenerList.remove(mylistener.class,listener);}注意:上面的listenerList 我在事件源中并没有定义,而是引用了超类中的这一变量。但是,如果在事件源中进行定义,也没有问题。还须和大家一起探讨。2、增加AWTEvent关于自定义事件的分发和处理public void processEvent(AWTEvent event){if(event instanceof myevent){EventListener[] listeners=listenerList.getListeners(mylistener.class);For(int i=0;i
原创粉丝点击