| 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 |