《GUI和AWT事件模型》由会员分享,可在线阅读,更多相关《GUI和AWT事件模型(2页珍藏版)》请在金锄头文库上搜索。
1、GUIAWTEventModelGUIGraphicUserInterfaceB/SServerHTTPServerC/SAWT(AbstractWindowTookit)SWINGJFrame,JPanel234FrameBorderLayoutPanelPanelFrameFlowLayoutAWTMotherMother600JDKEventSourceEventObjectEventListenerjava.util.EeventObjectjava.util.EventListenerclassMother/privateListlist=newArrayList();/public
2、voidaddHomeWorkListener(HomeWorkListenerhwl)list.add(hwl);/publicvoidremoveHomeWorkListener(HomeWorkListenerhwl)(list.remove(hwl);publicvoidnotify()(/6OCLOLKHomeWorkEventevent=newHomeWorkEvent(this);Iteratorit=list.iterator();while(it.hasNext()HomeWorkListenerh=(HomeWorkListener)it.next();h.homework
3、();classHomeWorkEventextendsEventObject/publicHomeWorkEvent(Objecto)super(o);interfaceHomeWorkListenerextendsEventListener/voidhomework(HomeWorkEvento);classXiaoQiangimplementsHomeWorkListener/1publicvoidhomework(HomeWorkEvento)/classXiaoMingimplementsHomeWorkListener/2publicvoidhomework(HomeWorkEve
4、nto)(/classXiaoFendimplementsHomeWorkListener/3publicvoidhomework(HomeWorkEvento)/XXXEventHomeWorkEventXXXListenerHomeWorkListenergetSource()1Gril13579(Boy)Boy246810BoyGrilEmotionEventextendsEventObjectEmotionListenerBoyCoding:publicclassGril/privateStringname;privateListlist=newArrayList();/publicG
5、ril(Stringname)=name;publicStringgetName()returnname;*/publicvoidaddEmotionListener(EmotionListenere)(list.add(e);/*/publicvoidremoveEmotionListener(EmotionListenere)(list.remove(e);/publicvoidfire()(EmotionEventevent=newEmotionEvent(this);for(inti=0;i10;i+)if(i%2=0)Iteratorit=list.iterator();while(
6、it.hasNext()EmotionListenerel=(EmotionListener)it.next();el.whatCanldoWhenHappy(event);elseIteratorit=list.iterator();while(it.hasNext()EmotionListenerel=(EmotionListener)it.next();el.whatCanldoWhenSad(event);/publicclassEmotionEventextendsEventObject(publicEmotionEvent(Objecto)(super(o);/publicinte
7、rfaceEmotionListenerextendsEventListener(voidwhatCanIdoWhenHappy(EmotionEvente);voidwhatCanIdoWhenSad(EmotionEvente);/:BoypublicclassBoyimplementsEmotionListener(privateStringname;publicBoy(Stringname)(=name;publicStringgetName()(returnname;publicvoidwhatCanIdoWhenHappy(EmotionEvente)(Objecto=e.getS
8、ource();Grilg=(Gril)o;System.out.println(name+saidto+g.getName()+,youhappy,Iamhappy);publicvoidwhatCanIdoWhenSad(EmotionEvente)Objecto=e.getSource();Grilg=(Gril)o;System.out.println(name+saidto+g.getName()+,yousad,Iamsosad.);Time,2022OlympicsWorldCupOlympicsWorldCupCoding:classTime/privateListlt=new
9、ArrayList();/privateintyear;/publicintgetYear()returnyear;publicvoidaddTimeListener(TimeListenert)lt.add(t);publicvoidremoveTimeListener(TimeListenert)lt.remove(t);publicvoidaction()TimeEventevent=newTimeEvent(this);for(inti=2022;i=3000;i+)this.year=i;/Iteratorit=lt.iterator();while(it.hasNext()Time
10、Listenertl=(TimeListener)it.next();/disp()tl.disp(event);/classTimeEventextendsEventObject(publicTimeEvent(Objecto)(super(o);/interfaceTimeListenerextendsEventListener(voiddisp(TimeEventevent);/1classOlympicsimplementsTimeListener(publicvoiddisp(TimeEventevent)(Timet=(Time)event.getSource();if(t.getYear()%4=0)(System.out.println(t.getYear()+);/2classWorldCupimplementsTimeListener(publicvoiddisp(TimeEventevent)(Timet=(Time)event.getSource();if(t.getYear()%4=2)(System.out.println(t.getYear()+);1N77