Java 事件处理实例
目的:学习Java Event触发,产生,发送,接处过程的处理思路。
方法实例:
1)模型
从上面模型中可以看出,EventSender是Event的产生和发送者,FooEvent是事件的原型,FooListener是事件接收的标准接口,FooListenerBase是事件接收者的Abstract实现,Receiver是事件真实收益者,其中有个匿名实现Extends FooListenerBase,而Client是事件的促进人,他创建条件来促进事件的触发。
2)调用过程
3)代码实现
3.1 Client.java
1 /* Generated by Together */
2
3 package com.bona.demos.event;
4
5 public class FooEvent {
6 public FooEvent(EventSender o) {
7 this.sender = o;
8 }
9
10 public EventSender getSender() {
11 return sender;
12 }
13
14 private EventSender sender = null;
15 }
16
3.4 FooListener.java
3.5 Receiver.java
3.6 FooListenerBase.java
方法实例:
1)模型
从上面模型中可以看出,EventSender是Event的产生和发送者,FooEvent是事件的原型,FooListener是事件接收的标准接口,FooListenerBase是事件接收者的Abstract实现,Receiver是事件真实收益者,其中有个匿名实现Extends FooListenerBase,而Client是事件的促进人,他创建条件来促进事件的触发。
2)调用过程
3)代码实现
3.1 Client.java
1 /* Generated by Together */
2
3 package com.bona.demos.event;
4
5 import javax.swing.event.EventListenerList;
6
7 public final class Client {
8 public static void main(String[] args) {
9 EventSender s = new EventSender();
10 s.setName("Event sender");
11 Receiver r = new Receiver();
12 s.addFooListener(r.getFoo());
13 s.fireFooXXX();
14 }
15 }
16
3.2 EventSender.java2
3 package com.bona.demos.event;
4
5 import javax.swing.event.EventListenerList;
6
7 public final class Client {
8 public static void main(String[] args) {
9 EventSender s = new EventSender();
10 s.setName("Event sender");
11 Receiver r = new Receiver();
12 s.addFooListener(r.getFoo());
13 s.fireFooXXX();
14 }
15 }
16
1 /* Generated by Together */
2
3 package com.bona.demos.event;
4
5 import javax.swing.event.EventListenerList;
6
7 public class EventSender {
8
9
10 public void fireFooXXX() {
11 FireFoo fire = new FireFoo();
12 //fire.fireFooXXX1();
13 fire.fireFooXXX2();
14 }
15
16 public void addFooListener(FooListener l) {
17 listenerList.add(FooListener.class, l);
18 }
19
20 public void removeFooListener(FooListener l) {
21 listenerList.remove(FooListener.class, l);
22 }
23
24 public String getName() { return name; }
25
26 public void setName(String name) { this.name = name; }
27
28 EventListenerList listenerList = new EventListenerList();
29 private String name;
30
31 class FireFoo {
32 FooEvent fooEvent = null;
33
34 // Notify all listeners that have registered interest for
35 // notification on this event type. The event instance
36 // is lazily created using the parameters passed into
37 // the fire method.
38
39 /*
40 public void fireFooXXX1() {
41 // Guaranteed to return a non-null array
42 Object[] listeners = listenerList.getListenerList();
43 // Process the listeners last to first, notifying
44 // those that are interested in this event
45 for (int i = listeners.length - 2; i >= 0; i -= 2) {
46 if (listeners[i] == FooListener.class) {
47 // Lazily create the event:
48 if (fooEvent == null)
49 fooEvent = new FooEvent(EventSender.this);
50 ((FooListener)listeners[i + 1]).fooXXX(fooEvent);
51 }
52 }
53 }
54 */
55
56
57 public void fireFooXXX2() {
58 Object[] listeners = listenerList.getListeners(FooListener.class);
59 for (int i = 0; i < listeners.length; i++) {
60 if (fooEvent == null)
61 fooEvent = new FooEvent(EventSender.this); // <==创建事件实例
62 ((FooListener)listeners[i]).fooXXX(fooEvent); // <==调用事件接口
63 }
64 }
65 }
66 }
67
3.3 FooEvent.java2
3 package com.bona.demos.event;
4
5 import javax.swing.event.EventListenerList;
6
7 public class EventSender {
8
9
10 public void fireFooXXX() {
11 FireFoo fire = new FireFoo();
12 //fire.fireFooXXX1();
13 fire.fireFooXXX2();
14 }
15
16 public void addFooListener(FooListener l) {
17 listenerList.add(FooListener.class, l);
18 }
19
20 public void removeFooListener(FooListener l) {
21 listenerList.remove(FooListener.class, l);
22 }
23
24 public String getName() { return name; }
25
26 public void setName(String name) { this.name = name; }
27
28 EventListenerList listenerList = new EventListenerList();
29 private String name;
30
31 class FireFoo {
32 FooEvent fooEvent = null;
33
34 // Notify all listeners that have registered interest for
35 // notification on this event type. The event instance
36 // is lazily created using the parameters passed into
37 // the fire method.
38
39 /*
40 public void fireFooXXX1() {
41 // Guaranteed to return a non-null array
42 Object[] listeners = listenerList.getListenerList();
43 // Process the listeners last to first, notifying
44 // those that are interested in this event
45 for (int i = listeners.length - 2; i >= 0; i -= 2) {
46 if (listeners[i] == FooListener.class) {
47 // Lazily create the event:
48 if (fooEvent == null)
49 fooEvent = new FooEvent(EventSender.this);
50 ((FooListener)listeners[i + 1]).fooXXX(fooEvent);
51 }
52 }
53 }
54 */
55
56
57 public void fireFooXXX2() {
58 Object[] listeners = listenerList.getListeners(FooListener.class);
59 for (int i = 0; i < listeners.length; i++) {
60 if (fooEvent == null)
61 fooEvent = new FooEvent(EventSender.this); // <==创建事件实例
62 ((FooListener)listeners[i]).fooXXX(fooEvent); // <==调用事件接口
63 }
64 }
65 }
66 }
67
1 /* Generated by Together */
2
3 package com.bona.demos.event;
4
5 public class FooEvent {
6 public FooEvent(EventSender o) {
7 this.sender = o;
8 }
9
10 public EventSender getSender() {
11 return sender;
12 }
13
14 private EventSender sender = null;
15 }
16
3.4 FooListener.java
1 /* Generated by Together */
2
3 package com.bona.demos.event;
4
5 import java.util.EventListener;
6
7 public interface FooListener extends EventListener {
8 void fooXXX(FooEvent event);
9 }
10
2
3 package com.bona.demos.event;
4
5 import java.util.EventListener;
6
7 public interface FooListener extends EventListener {
8 void fooXXX(FooEvent event);
9 }
10
3.5 Receiver.java
1 /* Generated by Together */
2
3 package com.bona.demos.event;
4
5 public class Receiver {
6 public Receiver() {
7 foo = new FooListenerBase() {
8 public void fooXXX(FooEvent e) {
9 System.out.println(e.getSender().getName());// <==事件接收开始
10 }
11 };
12 }
13
14 public FooListener getFoo() {
15 return foo;
16 }
17
18 private FooListener foo = null;
19 }
20
2
3 package com.bona.demos.event;
4
5 public class Receiver {
6 public Receiver() {
7 foo = new FooListenerBase() {
8 public void fooXXX(FooEvent e) {
9 System.out.println(e.getSender().getName());// <==事件接收开始
10 }
11 };
12 }
13
14 public FooListener getFoo() {
15 return foo;
16 }
17
18 private FooListener foo = null;
19 }
20
3.6 FooListenerBase.java
1 /* Generated by Together */
2
3 package com.bona.demos.event;
4
5 abstract public class FooListenerBase implements FooListener {
6 abstract public void fooXXX(FooEvent event);
7 }
8
2
3 package com.bona.demos.event;
4
5 abstract public class FooListenerBase implements FooListener {
6 abstract public void fooXXX(FooEvent event);
7 }
8
posted on 2006-05-24 14:15 Bona shen 阅读(4941) 评论(0) 编辑 收藏 举报