[ ^ CSE 341 | section index | <-- previous | next -->]

Update 27 Apr: Fixed erratum in BorderLayout comment.


Abstract Windowing Toolkit

Like all non-trivial graphical systems, the AWT consists of a lot of components, and learning how to use them all basically means reading a lot of API documentation.

However, there are certain interesting concepts that cut across the java.awt packages as a whole. I'll discuss two important ideas: the container/layout manager framework, and the event model.

Containers and components

The base class of all onscreen "components" (buttons, checkboxes, windows, etc.) is Component. Components are nested within Container objects, which are also components! Therefore, any Container can also be treated as a Component by all parts of the AWT. This is an example of the "Composite" design pattern (Gamma/Helm/Johnson/Vlissides, 1995).

The top-level container is usually a Frame, which represents a window as a user usually thinks of it: a square of space with an attached title bar, etc.

Since the AWT must be portable, programmers do not specify exact layouts. Instead, that task is delegated to the classes that implement the LayoutManager interface. Here's a sample usage:

import java.awt.*; public class MakeFrameWithButtons { public static void main(String[] args) { Frame f = new Frame(); // Create a top-level window // Use a common layout manager with 5 slots f.setLayout(new BorderLayout()); // Add a set of components f.add( new Button("Foo"), BorderLayout.NORTH); f.add( new Button("Bar"), BorderLayout.CENTER); f.add( new Button("Baz"), BorderLayout.EAST); f.add( new Button("Bif"), BorderLayout.WEST); f.pack(); // Invokes the layout manager f.setVisible(true); // Sets the frame visible } }

Last modified: Mon Jun 26 11:34:02 PDT 2000