AWT stands for Abstract Window Toolkit. It is
a Java package that can be imported as
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
Graphicsclass is the most important one and defines methods for line and text drawing and image painting. Image processing is supported by the
Imageclass. The classes
Polygoncontain graphics primitives for points, rectangles, and polygons, respectively. Other classes like
Colordefine details of the graphical display of components.
MenuComponentare the root classes. Their subclasses represent visual elements of a graphical user interface.
Componentis an abstract class in Java, so that you can only create objects belonging to its subclasses. Subclasses that represent basic GUI components are:
Canvas, and so on.
Container class is a special subclass
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.
Appletdoes not exist independently: a Web browser or appletviewer is needed to display it. A
Panel, like an
Appletdoes not stand on its own: a
Panelmust be contained inside something else, either a
Panel, or - in case of an
Applet- in a Web page.
Window represents an independent top-level window that is not
contained in any other component. Java distinguishes two kinds of windows:
MenuComponent class provides all means to
define menu bars and their elements.
LayoutManagerdeclares 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.
CardLayout, which places components on top of each other like a deck of cards and flips through the components.
GridLayoutwhich places components in rows and columns after resizing all of them to the same size.
GridBagLayoutwhich places components in rows and columns without requiring them to be the same size.
Eventclass 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
actionand by all mouse and key related methods of the
Componentclass, which is the root in the hierarchy of GUI components.
LayoutManagerand its role in the hierarchy is displayed by dotted lines. The classes
Appletare not actually part of the AWT package. They belong to the
java.appletpackage, respectively, but they have been included in the diagram to show their position in the total class hierarchy relative to the classes in the
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.