1. Overview

The AWT is the part of the Java environment that contains the set of classes for writing GUI (Graphic User Interface) Graphic programs:

Figure 23.1. The AWT

The AWT

  • The Basic Controls: Buttons, Checkboxes, Choices, Lists, Menus, and Text Fields

    When a user activates one of these controls, it posts an Action event to be handled by the object that contains the control.

  • Other Ways of Getting User Input: Sliders, Scrollbars, and Text Areas

    The Scrollbar class is used for both slider and scrollbar functionality. The TextArea class simply provides an area to display or edit text.

  • Creating Custom Components: Canvases

    With your Canvas subclass, you can draw custom graphics to the screen and implement any kind of event handling.

  • Labels

    A Label simply displays an unselectable line of text.

  • Containers: Windows and Panels

    The Window subclasses provide windows to contain components. Panels group components within an area of an existing window.

Audio in Portuguese

Figure 23.2. Converter of distances between US and metric.

Converter of distances between US and metric.

Figure 23.3. Classes in the Example Program

Classes in the Example Program

  • The Converter class actually extends the Applet class (which itself extends Panel), instead of directly extending Panel.

  • The ConversionPanel class provides a way of grouping all the controls that describe a particular set of distance measurements.

  • The Unit class provides objects that group a description (such as "Centimeters") with a multiplier that indicates the number of units per meter (0.01, for example).

Audio in Portuguese

                                Frame
                                  |
                                 ...
                                  |
                               Converter
                                  |
                ----------------------------------
                |                                |
       ConversionPanel (metricPanel)    ConversionPanel (usaPanel)   
                |                                |
        -------------------              -------------------
        |       |         |              |       |         |
      Label     |       Choice         Label     |       Choice
                |                                |
          --------------                  --------------
          |            |                  |            |
      TextField    Scrollbar          TextField    Scrollbar

1.4. Drawing

  • When the Converter application draws itself, here's what happens

    1. The Frame draws itself.

    2. The Converter object draws itself, drawing a box around its area.

    3. One of the two ConversionPanels draws itself, drawing a box around its area.

    4. The contents of the ConversionPanel -- the Label, TextField, Scrollbar, and Choice -- draw themselves.

  • How Drawing Requests are handled

    AWT ----> Component's update() ----> Component's paint()

  • Paint method

    public void paint(Graphics g) {
           Dimension d = size();
           g.drawRect(0,0, d.width - 1, d.height - 1);
    }

Audio in Portuguese