Chapter 23: Introducing the AWT: Working with Windows, Graphics, and Text 667
components that it contains. It does this through the use of various layout managers, which
you will learn about in Chapter 24.
Panel
ThePanelclass is a concrete subclass ofContainer. It doesn’t add any new methods; it simply
implementsContainer. APanelmay be thought of as a recursively nestable, concrete screen
component.Panelis the superclass forApplet. When screen output is directed to an applet,
it is drawn on the surface of aPanelobject. In essence, aPanelis a window that does not
contain a title bar, menu bar, or border. This is why you don’t see these items when an applet
is run inside a browser. When you run an applet using an applet viewer, the applet viewer
provides the title and border.
Other components can be added to aPanelobject by itsadd( )method (inherited from
Container). Once these components have been added, you can position and resize them
manually using thesetLocation( ),setSize( ),setPreferredSize( ),orsetBounds( )methods
defined byComponent.
Window
TheWindowclass creates a top-level window. Atop-level windowis not contained within any
other object; it sits directly on the desktop. Generally, you won’t createWindowobjects
directly. Instead, you will use a subclass ofWindowcalledFrame, described next.
Frame
Frameencapsulates what is commonly thought of as a “window.” It is a subclass ofWindow
and has a title bar, menu bar, borders, and resizing corners. If you create aFrameobject from
within an applet, it will contain a warning message, such as “Java Applet Window,” to the
user that an applet window has been created. This message warns users that the window
they see was started by an applet and not by software running on their computer. (An applet
that could masquerade as a host-based application could be used to obtain passwords and
other sensitive information without the user ’s knowledge.) When aFramewindow is created
by a stand-alone application rather than an applet, a normal window is created.
Canvas
Although it is not part of the hierarchy for applet or frame windows, there is one other type
of window that you will find valuable:Canvas.Canvasencapsulates a blank window upon
which you can draw. You will see an example ofCanvaslater in this book.
Working with Frame Windows
After the applet, the type of window you will most often create is derived fromFrame. You will
use it to create child windows within applets, and top-level or child windows for stand-alone
applications.As mentioned, it creates a standard-style window.
Here are two ofFrame’s constructors:
Frame( )
Frame(Stringtitle)
The first form creates a standard window that does not contain a title. The second form creates
a window with the title specified bytitle.Notice that you cannot specify the dimensions of
the window. Instead, you must set the size of the window after it has been created.