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