(七)GUI和AWT事件模型

更新时间:2023-05-25 07:39:01 阅读量: 实用文档 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

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

AWT事件模型

Coding:

class Time { //

private List lt = new ArrayList();//

private int year; //

public int getYear() { return year; }

public void addTimeListener(TimeListener t) {

lt.add(t);

}

public void removeTimeListener(TimeListener t) {

lt.remove(t);

}

public void action() {

TimeEvent event = new TimeEvent(this);

for(int i=2006;i<=3000;i++) {

this.year = i; //

Iterator it = lt.iterator();

while(it.hasNext()) {

TimeListener tl = (TimeListener)it.next();

// disp()

tl.disp(event);

}

}

}

}

//

class TimeEvent extends EventObject {

public TimeEvent(Object o) {

super(o);

}

}

//

interface TimeListener extends EventListener {

void disp(TimeEvent event);

}

// 1

class Olympics implements TimeListener {

public void disp(TimeEvent event) {

Time t = (Time)event.getSource();

if(t.getYear() % 4 == 0 ){

System.out.println(t.getYear()+” ”);

}

}

}

// 2

AWT事件模型

class WorldCup implements TimeListener {

public void disp(TimeEvent event) {

Time t = (Time)event.getSource();

if(t.getYear() % 4 == 2 ) {

System.out.println(t.getYear()+” ”);

}

}

}

1 N 7

7

(Steven Ye)mailto: leton.ye@

本文来源:https://www.bwwdw.com/article/uvt4.html

Top