be placed and add the components so that they have the expected relationships. If you do this right, your
interface will look clean on all platforms.
AWT has a set of standard GUI components: labels, buttons, check boxes, choice lists, scroll bars, text fields,
text regions, and so on. Several top-level containers, such as dialog boxes and windows, let you place other
components inside them (preferably using a relative-placement layout manager).
When you need to draw your own graphics on the screen, the simplest technique is to subclass Component
or Container, overriding the paint method to draw what you need using the Graphics object passed to
paint. Component is the basis for many customized user-interface components when no standard
component does what is needed. You would subclass Container if your drawn component contained other
components.
The AWT is an event-based system. When the user performs an action, such as moving a mouse, clicking a
mouse button, or typing a key, an event is generated that identifies the underlying component as the source of
that event and that encapsulates the nature of that event. The event is then placed in a central event queue, and
will be serviced by a single event thread. The event thread takes events from the queue one at a time and
dispatches them to their source components. You can also generate events programmaticallyfor example,
invoking repaint on a component puts an UPDATE event into the event queueor can create event objects
and insert them directly into the event queue.
Components are notified of events (mouse up, mouse down, mouse drag, keyboard events, and the like) by
having methods invoked on them by the event thread. You can also register listener objects that are notified
when an event occurs within a component. For example, a button that lets the user exit the program might be
set up like this:
Button b = new Button("Exit");
b.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
);
gui.add(b);
Action events come from the basic action on the component, such as pressing a button or selecting a check
box. The ActionEvent class has details of an action event, such as the keyboard modifiers (such as the Alt
or Control key) that were being pressed when the action occurred. To receive this event you must register
interest in action events, either by adding an ActionListener to the component as shown here or by
invoking enableEvents with an appropriate mask.
The ActionListener interface in the previous example is a listener interface. Listener interfaces extend
java.util.EventListener and exist for many kinds of events: mouse, keyboard, window, item, text,
container, and general component events. You use objects that implement listener interfaces to trigger method
execution when events occurall of which get executed by the event thread. When an event occurs, the
component's processEvent method is called, which in turn calls processXXXEvent for the specific
event, and that in turn will call the XXXOccurred method of all registered listeners for that event.
Other classes in java.awt allow you to set colors, fonts, and so on. All these properties are inherited by
default from outer componentsthe default colors of a button are those of the frame in which it has been placed.
You can override this setting at any level by explicitly specifying a color for a component. That component
and all its contained components will change to the specified color unless a subcomponent has its color
specified.