赞
踩
《Java语言程序设计:基础篇》课后复习题答案-第十五章
Chapter15Graphics
1. Theycoordinateshouldincreaseandthexcoordinateshouldremainunchanged.
2. The Graphics class–an abstract class—provides a
device-independent graphics interface for displaying
figures and images on the screen on different
platforms. Whenever a component (e.g., a button, a
label, a panel) is displayed, the JVM automatically
creates a Graphics object for the component on the
native platform and passes this object to invoke the
paintComponent method to display the drawings.
3. ThepaintComponent()methodisdefinedintheComponentclass.TheJava
runtimesysteminvokesittopaintthingsonaSwingGUIcomponent.This
methodcannotbeinvokedbythesystemorbytheprogrammer.Thesystem
automaticallyinvokesitwhenevertheviewingareachanges.Theprogrammer
invokesitthroughinvokingtherepaint()method.Theprogrammershouldnever
directlyinvokethepaintComponent()method.
4. ThepaintComponent()methodisprotected,because(1)thismethodisalways
invokedbytheJVM,notbyaclientprogram;(2)theclientprogramneedto
overrideitinasubclass.
Ifitischangedtopublic,itisOK,butnotnecessary,becausetheprotected
modifierissufficient.
Itcannotbechangedtoprivate,becausethevisibilitycannotbeweakened.
super.paintComponent(g)invokesthesuperclass’spaintComponentmethod.In
Line 12inListing 12.2,itcausesthetextofthelabeltobepaintedfirst.Before
thistextisdisplayed,theJLabel’spaintComponent(g)methodactuallyinvokes
super.paintComponent(g)tocleartheviewingarea.InLine20inListing 12.3,it
causestheviewingareatobecleared.
5. Yes.YoushoulddeclareacustomcanvasbysubclassingJPanelratherthan
subclassingJLabelorJButton,becauselabelsaredesignedfor
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。