《Java语言程序设计:基础篇》课后复习题答案-第十六章

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

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).

1 / 2
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功