Chapter16Event-DrivenProgramming1.AWindowEventisgeneratedbyaninstanceoftheWindowclassoritssubclass.JButtonisnotasubclassofWindow,therefore,itcannotgeneratetheWindowEvent.JButtoncangenerateMouseEventandActionEvent.2.Alistenermusthaveacorrectmethodtohandletheevent.Toensurethatalistenerhavethemethod,alistenermustbeaninstanceofalistenerinterface,whereamethodisdefined.Toregisteralistenerobject,youinvokethesourceobject'saddXListener'smethod;forexample,button.AddActionListener(this).Toimplementalistenerinterface,youaddimplementsXlistenerandimplementallthehandlersinthelistener'sobject.3.Yes.Yes.Yes.4.Tooverrideamethoddefinedinthelistenerinterface,youprovidethecodeinthebodyofthemethodforhandlingtheevent.Youneedtooverrideallthemethodsdefinedinthelistenerinterface.Ifahandlerisnotusedintheprogram,youcangiveitanemptybody.5.Objectsofaninnerclassareoftencreatedintheouterclass.Butyoucanalsocreateanobjectofaninnerclassfromanotherclass.Iftheinnerclassisnonstatic,youmustfirstcreateaninstanceoftheouterclass,thenusethefollowingsyntaxtocreateanobjectfortheinnerclass:OuterClass.InnerClassinnerObject=outerObject.newInnerClass();Iftheinnerclassisstatic,usethefollowingsyntaxtocreateanobjectforit:OuterClass.InnerClassinnerObject=newOuterClass.InnerClass();6.Yes.7.IfclassAisaninnerclassinclassB,the.classfileforAisB$A.class.IfclassBcontainstwoanonymousinnerclasses,the.classfilenamesforthesetwoclassesareB$+1andB$+2.8.(a)1.java.awt.eventpackageshouldbeimportedtouseActionEventandActionListenerinthisprogram;2.java.swingshouldbejavax.swing3.actionPerformshouldbeactionPerformed;5.jbtOKisnotinthescopeoftheactionPerformedmethod;5.nolistenerisregisteredwithjbtOK.(b)missing)inLine;9.ThesetSizemethodspecifiesthewidthandheightofaframe,whereasthepackmethodlettheJVMautomaticallysetsthesizefortheframeaccordingtothesizeofthecomponentsintheframe.10.UsegetSource()intheEventObjectclasstogetthesourceofanevent.UsegetWhen()intheActionEventclasstogetthetimestampforanactionevent,usegetWhen()intheInputEventclasstogetthetimestampamouseeventorakeyevent.UsegetPoint(),orgetX()andgetY()togetthemousepointpositionforamouseevent.UsegetKeyChar()togetthekeycharacterforakeyevent.11.Thelistenerinterfaceformovepressed,released,clicked,entered,andexitedisMouseListener.ThelistenerinterfaceformousemovedanddraggedisMouseMotionListener.12.NoteverykeyinthekeyboardhasaUnicode.Forexample,thefunctionkeys,modifierkeys,actionkeys,andcontrolkeysarenotUnicodecharacters.ThekeycodeintheKeyEventclassaredefinedasconstants.TheirvalueisthesameastheUnicodevalueifthekeyisUnicodecharacter.13.ThekeyPressedhandlerisinvokedafterakeyispressed.ThekeyReleasedhandlerisinvokedafterakeyisreleased.ThekeyTypedhandlerinvokedafteraUnicodecharacterkeyistyped.14.YoucreateatimerusingtheconstructorpublicTimer(intdelay,ActionListenerlistener)Usethestartmethodtostartthetimer,andthestopmethodtostopthetimer.15.TheTimerclassdoesnothaveano-argconstructor.Youcanaddmultiplelistenersbyinvokingthetimer.addActionListener(listener).