AWT stands for Abstract Window Toolkit. It is
a Java package that can be imported as java.awt.*
and that consists of platform-independent windowing, graphics, and
user interface tools for programmers to use when building up
applets and/or stand-alone Java applications.
The classes of the java.awt
package can be rougly divided
into:
Graphics
class is the most important one and defines methods for line and
text drawing and image painting. Image processing is supported by the
Image
class. The classes Point
,
Rectangle
and Polygon
contain graphics
primitives for points, rectangles, and polygons, respectively.
Other classes like Font
and Color
define details of the graphical display of components.
Component
and MenuComponent
are the root classes. Their subclasses
represent visual elements of a graphical user interface.
Component
is an abstract class in Java, so that you
can only create objects belonging to its subclasses. Subclasses that
represent basic GUI components are: Button
,
ScrollBar
, TextField
, Label
,
CheckBox
, Canvas
, and so on.
The Container
class is a special subclass
of Component
:
it is a type of component that can contain other components and
arranges them visually. The Container
class, like
Component
is an abstract class. It has two direct subclasses:
Panel
, which is only used for grouping components;Window
, which is, as its name says, for creating and handling
windows.Panel
is an Applet
.
An Applet
does not exist independently: a Web browser
or appletviewer is needed to display it. A Panel
, like an
Applet
does not stand on its own:
a Panel
must be contained inside something else, either a
Window
, another Panel
, or - in case of
an Applet
- in a Web page.
A Window
represents an independent top-level window that is not
contained in any other component. Java distinguishes two kinds of windows:
Dialog
;Frame
.
The MenuComponent
class provides all means to
define menu bars and their elements.
LayoutManager
declares methods of classes that control the layout of components within
containers. Predefined layouts are:
FlowLayout
, which places components
in a simple left-to-right order.BorderLayout
CardLayout
,
which places components on top of each other like a deck of cards and
flips through the components.GridLayout
which places components in rows and columns after resizing all of them to the
same size.GridBagLayout
which places components in rows and columns without requiring them to be
the same size.
Event
class is the most important one and it contains
public instance variables for use when handling GUI events and actions.
The actual handling of events and actions is taken care of by the
methods handleEvent
and action
and by all mouse
and key related methods of the Component
class,
which is the root in the hierarchy of GUI components.
LayoutManager
and its role in the hierarchy is
displayed by dotted lines. The classes Object
and
Applet
are not actually part of the AWT package. They
belong to the java.lang
and java.applet
package, respectively, but they have been included in the diagram
to show their position in the total class hierarchy relative
to the classes in the java.awt
package.
The way you use the AWT to build up a user interface is closely related to the AWT structure as descibed above: components (buttons, scrollbars, ...) are added to and then laid out by layout managers in containers (frames, dialogs, panels). The GUI components, layout managers, and graphics tools determine for the most part the outlook of the user interface. The event and action handling finally gives meaning to the components of the user interface.
Practical examples in this chapter and subsequent chapters shall illustrate the use of the AWT package for creating graphical user interfaces.