Home : Course Map : Chapter 7 : Java : Supplements :
A GUI with AWT Components
JavaTech
Course Map
Chapter 7

Introduction
Event Overview
Event Processing
Button Events
  Demo 1
 Demo 2
Mouse Events
  Demo3

More Components
  Demo 4  Demo 5
  Demo 6  Demo 7

LayoutManagers-1
  Demo 8     Demo 9
  Demo 10  Demo 11
  Demo 12

LayoutManagers-2
  Demo 13  Demo 14
  Demo 15  Demo 16
  Demo 17

Inner Classes
Anonymous Class
Adapter Classes
  Demo 18  Demo 19
Frames & Menus
  Demo 20  Demo 21
Exercises

    Supplements
AWT Components
  Button
     Demo 1
  Canvas
     Demo 2
  AWT GUI Demo
     Demo 3
Swing Dialogs
JOptionPane Dialog
  Demo 1
JDialog
  Demo 2
UI Enhancement: P1
  Demo 1   Demo 2
  Demo 3

UI Enhancement: P2
  Demo 1
     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

A standard graphical user interface for a program requires a frame with a border, menubars, dropdown menus, and vaious components in the body of the frame. The applet below illustrates the basics of creating a frame with a menubar and menus with the heavyweight AWT components. It includes a Choice component with a list. The event handling code shows how to respond to selection of items on the menus and choice list.

GUIApplet.java
 
import java.awt.*;
import java.applet.*;
import java.awt.event.*;

/**
  * This program illustrates how to do a Frame user interface
  * in the basic AWT framework rather than Swing.
**/
public class GUIApplet extends Applet
       implements ActionListener
{
  /** A button to create and open an instance of TheFrame. **/
  public void init () {
    // Create a button to make and display
    // an instance of TheFrame.
    Button button = new Button ("Make a Frame");
    button.addActionListener (this);
    add (button);
  } // init

  public void actionPerformed (ActionEvent e) {
    Frame f = new TheFrame ();
    f.setVisible (true);
  }

  // Allow for standalone operation.
  public static void main (String[] s) {
    TheFrame tf = new TheFrame ();
    tf.setVisible (true);
   }
} // class GUIApplet


/** This frame subclass offers button and
  * menu event handling.
**/
class TheFrame extends Frame
      implements ActionListener, ItemListener
{
  Label fLabelA;
  Label fLabelB;

  TheFrame () {

    setTitle ("An AWT Frame Example");

    setLayout (new GridLayout (1,3));

    Panel choice_panel = new Panel ();
    choice_panel.add (new Label ("Quark", Label.RIGHT));

    Choice c = new Choice ();
    c.addItem ("Up");
    c.addItem ("Down");
    c.addItem ("Strange");
    c.addItem ("Charm");
    c.addItem ("Top");
    c.addItem ("Bottom");
    c.addItemListener (this);
    choice_panel.add (c);

    add (choice_panel);

    fLabelA = new Label ("Quark: Up");
    add (fLabelA);
    fLabelB = new Label ("Lepton: Electron");
    add (fLabelB);


    // Use the helper method makeMenuItem
    // for making the menu items and registering
    // their listener.
    Menu m = new Menu ("Lepton");
    m.add (makeMenuItem ("Electron"));
    m.add (makeMenuItem ("Muon"));

    Menu sm = new Menu ("Neutrino");
    sm.add (makeMenuItem ("e Neutron"));
    sm.add (makeMenuItem ("mu Neutrino"));


    m.add (sm);
    m.add (makeMenuItem ("Quit"));
    MenuBar mb = new MenuBar ();
    mb.add (m);

    setMenuBar (mb);
    setSize (200,200);
    pack ();

    WindowListener listener =
      new WindowAdapter ()
      {
        public void windowClosing (WindowEvent e) {
          dispose ();
        }
      };
    addWindowListener (listener);

  } // ctor

  /** Get the combobox item events here. **/
  public void itemStateChanged (ItemEvent e) {
    String command = e.getItem ().toString ();
    if (command.equals ("Quit") )
        dispose ();
    else
        fLabelA.setText ("Quark: " + command);
  }

  /** Get the menu events here. **/
  public void actionPerformed ( ActionEvent e ) {
    String command = e.getActionCommand ();
    if (command.equals ("Quit")) {
        dispose ();
    } else {
        fLabelB.setText ("Lepton: " + command);
    }
  } // actionPerformed

  /** This "helper method" makes a menu item and then
    * registers this applet as a listener to it.
   **/
  private MenuItem makeMenuItem ( String name) {
    MenuItem m = new MenuItem ( name );
    m.addActionListener ( this );
    return m;
  }

} // class TheFrame

 

Latest update: Nov. 4, 2004

           Tech
Histogram UI
  Demo 1
Probablity Distrib.
  Demo 2 Demo 3
RejectionMethod
Histogram Stats
  Demo 4
Exercises

           Physics
Sim & Randomness
Custom Prob. Dist.
   Demo 1
Histogram Dist.
   Demo 2
Monte Carlo
  Demo 3
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.