Window Fundamentals

Window Fundamentals 

The AWT defines windows according to a hierarchy that adds functionality and
specificity With each level. The two most common windows are those derived from
Panel, which is used by applets, and those derived from Frame, which creates a standard
application window. Much Of the functionality Of these windows is derived from their
parent classes. Thus, a description of the class hierarchies relating to these two classes is
fundamental to their understanding. Figure  shows the class hierarchy for panel and
Frame. look at each of these classes now.



Component 

At the top of the AWT hierarchy is the Component class Component is an abstract class that
encapsulates all of the attributes of a visual component. All user interface elements that are
displayed on the screen and that interact With the user are subclasses Of Component. It defines
over a hundred public methods that are responsible for managing events, such as mouse and
keyboard input, positioning and sizing the window, and repainting.  A Component object is
responsible for remembering the current foreground and background colors and the currently
selected text font.

Container 

The Container class is a subclass of Component. It has additional methods that allow other
Component Objects to be nested within it. Other Container Objects can be stored inside Of a
Container (since they are themselves instances Of Component). This makes for a multileweled
containment system. A container is responsible for laying Out (that is, positioning) any
components that it contains. It does this through the Of various layout managers, which
you Will learn about in Chapter 24.

Panel 

The Panel class is a concrete subclass of Container. It doesn't add any new methods; it simply
implements Container. A Panel may be thought of as a recursively nestable, concrete screen
component. Panel is the superclass for Applet. When screen output is directed to an applet,
it is drawn on the surface Of a Panel Object. In essence, a Panel is a window that does not
contain a title bar, menu bar, or border. ms 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 a panel object by its add( method (inherited from
Container). Once these components have been added, you can position and resize them
manually using the setl.ocation( ), setSize( ), setPreferredSize( or ) methcds
defined by Component.

Window 

The Window class creates a top-level window. A top-level window is not contained within any
other Object; it sits directly on the desktop. Generally, you won't create Window objects
directly. Instead, you Will use a subclass Of Window called Frame, described next.

Frame 

Frame encapsulates what is commonly thought of as a "window." It is a of Window
and has a title bar, menu bar, borders, and resizing corners. If you create a Frame object 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 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 a Frame window 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. Canvas encapsulates a blank window upon 
which you can draw. You will see an example of Canvas later in this book.