The Java Program: JWidgets.java
1 import java.awt.*;
2 import java.awt.event.*;
3 import javax.swing.*;
4 public class JWidgets extends JFrame {
5 public static void main (String[] args) {
6 new JWidgets();
7 }
8 JLabel l; JButton b; Checkbox x; Choice ch;
9 JTextArea ta; JTextField tf; JList list; Scrollbar s;
10 private void jadd (Component c) { getContentPane().add (c); }
11 public JWidgets() {
12 setTitle ("JWidgets Demo");
13 getContentPane().setLayout(
14 new FlowLayout (FlowLayout.LEFT, 3, 3));
15 l = new JLabel ("Label", Label.RIGHT);
16 b = new JButton ("Button");
17 x = new Checkbox ("Checkbox");
18 ch = new Choice();
19 ch.addItem ("first choice");
20 ch.addItem ("second choice");
21 ch.addItem ("third choice");
22 // initial text value, row, cols
23 ta = new JTextArea ("TextArea", 3, 30);
24 // initial text value, cols
25 tf = new JTextField ("TextField", 30);
26 // rows, multiple selections
27 String [] data = {"one", "two", "three"};
28 list = new JList (data);
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 jadd (l); jadd (b); jadd (x); jadd (ch);
33 jadd (ta); jadd (tf); jadd (list); jadd (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 }