(七)GUI和AWT事件模型(2)
发布时间:2021-06-08
发布时间:2021-06-08
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@
上一篇:符号学在环境艺术设计中的应用
下一篇:18650电池规格书