手机版

(七)GUI和AWT事件模型

发布时间:2021-06-08   来源:未知    
字号:

AWT事件模型

GUI AWT Event Model

GUI Graphic User Interface

B/S Server HTTP

Server

C/S

AWT (Abstract Window Tookit)

SWING

1 JFrame, JPanel

2

3

4

Container Component Component ’

Frame BorderLayout

Panel Panel Frame FlowLayout

AWT

Mother

Mother 6 00

AWT事件模型

JDK

1 EventSource

2 EventObject

3 EventListener

java.util.EeventObject

java.util.EventListener

class Mother {//

private List list = new ArrayList();

//

public void addHomeWorkListener(HomeWorkListener hwl) {

list.add(hwl);

}

//

public void removeHomeWorkListener(HomeWorkListener hwl) {

list.remove(hwl);

}

AWT事件模型

public void notify() { //

//6 O CLOLK

HomeWorkEvent event = new HomeWorkEvent(this);

Iterator it = list.iterator();

while(it.hasNext()) {

HomeWorkListener h = (HomeWorkListener)it.next();

h.homework();

}

}

class HomeWorkEvent extends EventObject{ //

public HomeWorkEvent(Object o) {

super(o);

}

}

interface HomeWorkListener extends EventListener{ //

void homework(HomeWorkEvent o);

}

class XiaoQiang implements HomeWorkListener { // 1

public void homework(HomeWorkEvent o) {

//

}

}

class XiaoMing implements HomeWorkListener {// 2

public void homework(HomeWorkEvent o) {

//

}

}

class XiaoFend implements HomeWorkListener { // 3

public void homework(HomeWorkEvent o) {

//

}

}

XXXEvent HomeWorkEvent

XXXListener HomeWorkListener

getSource()

1 Gril 1 3 5 7 9 (Boy) Boy 2 4 6 8 10 Boy

AWT事件模型

1 Gril

2 EmotionEvent extends EventObject

3 EmotionListener

4 Boy

Coding:

publicclass Gril {//

private Stringname;

private Listlist =new ArrayList();//

public Gril(String name) {

= name;

}

public String getName() {

returnname;

}

/***************************************************

*

*@parame:

*/

publicvoid addEmotionListener(EmotionListener e) {

list.add(e);

}

/*********************************************************

*

*@parame

*/

publicvoid removeEmotionListener(EmotionListener e) {

list.remove(e);

}

//

publicvoid fire() {

EmotionEvent event =new EmotionEvent(this);

for (int i = 0; i < 10; i++) {

if(i % 2 == 0 ) {

Iterator it =list.iterator();

while(it.hasNext()) {

EmotionListener el =

(EmotionListener)it.next();

el.whatCanIdoWhenHappy(event);

}

}else {

Iterator it =list.iterator();

while(it.hasNext()) {

EmotionListener el =

AWT事件模型

(EmotionListener)it.next();

el.whatCanIdoWhenSad(event);

}

}

}

}

}

//

publicclass EmotionEventextends EventObject {

public EmotionEvent(Object o) {

super(o);

}

}

//

publicinterface EmotionListenerextends EventListener {

void whatCanIdoWhenHappy(EmotionEvent e);

void whatCanIdoWhenSad(EmotionEvent e);

}

// : Boy

publicclass Boyimplements EmotionListener {

private Stringname;

public Boy(String name) {

= name;

}

public String getName() {

returnname;

}

publicvoid whatCanIdoWhenHappy(EmotionEvent e) {

Object o = e.getSource();

Gril g = (Gril)o;

System.out.println(name+" said to "+g.getName()+", youhappy,I am happy");

}

publicvoid whatCanIdoWhenSad(EmotionEvent e) {

Object o = e.getSource();

Gril g = (Gril)o;

System.out.println(name+" said to "+g.getName()+", yousad,I am so sad.");

}}

Time , 2006 Olympics WorldCup Olympics WorldCup

(七)GUI和AWT事件模型.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
×
二维码
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)