(七)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文档下载到电脑

精彩图片

热门精选

大家正在看

× 游客快捷下载通道(下载后可以自由复制和排版)

限时特价:7 元/份 原价:20元

支付方式:

开通VIP包月会员 特价:29元/月

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219