Java Reference
In-Depth Information
or specifies the textfield's width in terms of displayable columns, where a
column
is
defined as an approximate average character width (and is platform-dependent).
Thefinalgridrowpresentsapairof
Button
instancesforperformingconversions.
Eachinstanceisassignedanactionlistenerthatrespondstoabuttonclickbyobtaining
the top textfield's text (via
TextField
's
String getText()
method, which is
inherited from
TextField
's
TextComponent
superclass), converting it to a num-
ber,andassigningittothebottomtextfieldbycalling
TextField
'soverriding
void
setText(String t)
method.
After populating the panel, the constructor adds the panel to the frame window. It
then invokes
pack()
to ensure that the frame window is made large enough to dis-
play its components at their preferred sizes, invokes
Frame
's
void setResiz-
able(boolean resizable)
methodwitha
false
argumenttopreventtheuser
from resizing the frame window (and making it look ugly), and invokes
setVis-
ible()
with a
true
argument to display the frame and its components.
Aftertheconstructorreturnsto
main()
,thisclassmethodexits.However,theframe
windowremainsonthescreenbecauseit'sconnectedtoanativescreenresourceandbe-
cause the running EDT is a nondaemon thread (discussed in
Chapter 4
).
TempVerter
).
Figure 7-4
shows the resulting GUI on the Windows XP platform.
Figure 7-4.
Click the X button to close this window and terminate the application.
WhenyouenternonnumerictextorleavetheDegreestextfieldempty,
TempVerter
outputsa“
bad input
”messagetothestandardoutputdevice.Also,whenyouclose
the window, this application outputs “
window closing
” followed by “
window
closed
” messages on separate lines.
Note
You can move forward to the next component by pressing the Tab key, and
movebackwardtothepreviouscomponentbypressingShift-Tab.Thecomponentthat
youtabtohasthe
focus
whenitcanobtaininput—theonly
TempVerter
components
capableofreceivingfocusarethetwotextfieldsandthetwobuttons.Whenyoudisable