The Java Program: Widgets.java

  1 import java.awt.*;
  2 import java.awt.event.*;
  3 public class Widgets extends Frame {
  4    public static void main (String[] args) {
  5       new Widgets();
  6    }
  7    Label l;       Button b;       Checkbox x;     Choice ch;
  8    TextArea ta;   TextField tf;   List list;      Scrollbar s;   
  9 
 10    public Widgets() {
 11       setTitle ("Widgets Demo");
 12       setLayout (new FlowLayout (FlowLayout.LEFT, 3, 3));
 13       l = new Label ("Label", Label.RIGHT);
 14       b = new Button ("Button"); 
 15       x = new Checkbox ("Checkbox");
 16       ch = new Choice();
 17       ch.addItem ("first choice");
 18       ch.addItem ("second choice");
 19       ch.addItem ("third choice");
 20       // initial text value, row, cols
 21       ta = new TextArea ("TextArea", 3, 30);
 22       // initial text value, cols
 23       tf = new TextField ("TextField", 30);
 24       // rows, multiple selections
 25       list = new List (3, true);
 26       list.add ("one");
 27       list.add ("two");
 28       list.add ("three");
 29       // orientation, initial value, delta on click, min, maximum value
 30       s = new Scrollbar (Scrollbar.HORIZONTAL, 50, 10, 0, 100);
 31       // insert all the widgets in the applet aligned on left margin
 32       add (l);    add (b);    add (x);     add (ch);
 33       add (ta);   add (tf);   add (list);  add (s);
 34       class WindowClosingListener extends WindowAdapter {
 35          public void windowClosing (WindowEvent evt) { close(); }
 36       }
 37       addWindowListener (new WindowClosingListener ());
 38       setSize (450, 200);
 39       setVisible (true);
 40    }
 41 
 42    void close () {
 43       setVisible (false);
 44       dispose();
 45       System.exit(0);
 46    }
 47 }