Java Reference
In-Depth Information
user enter characters in a textfield). Passing
false
to this method prevents
thecomponentfromreceivinginputfocus,andIdosoonthevarioustextfield/
textarea components for this purpose. Although I could have called
setEn-
abled(false)
toachievethesameresult,Ididn'tdothisbecauseadisabled
textfield's/textarea'stextappearsfaintandishardtoread(atleastunderthede-
faultMetalLookandFeel).Incontrast,thetextisstrongandeasytoreadwhen
the component is not focusable.
• The
java.awt.BorderLayout
class is used extensively to lay out the
GUI. It lets you arrange up to five components in the north, south, east, west,
and center areas of its associated container. Components are laid out accord-
ingtotheirpreferredsizesandtheconstraintsofthecontainer'ssize.Thenorth
and south components may be stretched horizontally; the east and west com-
ponentsmaybestretchedvertically;thecentercomponentmaystretchhorizont-
ally and vertically to fill any space left over. When adding a component to a
containerthat'smanagedbyaborderlayout,
java.awt.Container
's
void
add(Component comp, Object constraints)
method is called
with one of
BorderLayout
's
java.lang.String
-based constraint con-
stants(e.g.,
NORTH
)asthesecondargument,totellthelayoutmanagerwhere
to place the component.
ThelistenersattachedtotheConnect,Send,andQuitbuttonsshowyouhowtocreate
a socket that connects to the chat server, communicate with the chat server, and close
the socket.
ChatServer
and
ChatClient
communicateoverthesameportnumber(8010).
Also,
ChatClient
assumes that
ChatServer
is running on the same computer
byspecifying localhost (127.0.0.1).If
ChatServer
ranonadifferent computer,you
would specify that computer's domain name/IP address instead.
isrunning,startapairof
ChatClient
instancesbyexecuting
java ChatClient
in two different command windows.
Figure 9-2
shows users Jack and Jill communicating over their chat clients.