listing 1
// Demonstrate Labels.
import java.awt.*; 
import java.awt.event.*;
 
public class LabelDemo extends Frame { 
  public LabelDemo() { 

    // Use a flow layout.
    setLayout(new FlowLayout());

    Label one = new Label("One"); 
    Label two = new Label("Two"); 
    Label three = new Label("Three"); 
 
    // Add labels to frame.
    add(one); 
    add(two); 
    add(three); 

    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
        System.exit(0);
      }
    });
  } 

  public static void main(String[] args) {
    LabelDemo appwin = new LabelDemo();

    appwin.setSize(new Dimension(300, 100));
    appwin.setTitle("LabelDemo");
    appwin.setVisible(true);
  } 
}

listing 2
// Demonstrate Buttons. 
import java.awt.*; 
import java.awt.event.*; 
 
public class ButtonDemo extends Frame implements ActionListener { 
  String msg = ""; 
  Button yes, no, maybe; 
 
  public ButtonDemo() { 

    // Use a flow layout.
    setLayout(new FlowLayout());

    // Create some buttons.
    yes = new Button("Yes"); 
    no = new Button("No"); 
    maybe = new Button("Undecided"); 
 
    // Add them to the frame.
    add(yes); 
    add(no); 
    add(maybe); 
 
    // Add action listeners for the buttons.
    yes.addActionListener(this); 
    no.addActionListener(this); 
    maybe.addActionListener(this); 

    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
        System.exit(0);
      }
    });
  } 

  // Handle button action events. 
  public void actionPerformed(ActionEvent ae) { 
    String str = ae.getActionCommand(); 
    if(str.equals("Yes")) { 
      msg = "You pressed Yes."; 
    } 
    else if(str.equals("No")) { 
      msg = "You pressed No."; 
    } 
    else { 
      msg = "You pressed Undecided."; 
    } 
 
    repaint(); 
  } 
 
  public void paint(Graphics g) { 
     g.drawString(msg, 20, 100); 
  } 

  public static void main(String[] args) {
    ButtonDemo appwin = new ButtonDemo();

    appwin.setSize(new Dimension(250, 150));
    appwin.setTitle("ButtonDemo");
    appwin.setVisible(true);
  } 
}

listing 3
// Recognize Button objects. 
import java.awt.*; 
import java.awt.event.*; 
 
public class ButtonList extends Frame implements ActionListener { 
  String msg = ""; 
  Button bList[] = new Button[3]; 
 
  public ButtonList() { 

    // Use a flow layout.
    setLayout(new FlowLayout());

    // Create some buttons.
    Button yes = new Button("Yes"); 
    Button no = new Button("No"); 
    Button maybe = new Button("Undecided"); 
 
    // Store references to buttons as added.  
    bList[0] = (Button) add(yes); 
    bList[1] = (Button) add(no); 
    bList[2] = (Button) add(maybe); 
 
    // Register to receive action events.
    for(int i = 0; i < 3; i++) { 
      bList[i].addActionListener(this); 
    } 

    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
        System.exit(0);
      }
    });
  } 
 
  // Handle button action events. 
  public void actionPerformed(ActionEvent ae) { 
    for(int i = 0; i < 3; i++) { 
      if(ae.getSource() == bList[i]) { 
        msg = "You pressed " + bList[i].getLabel(); 
      } 
    } 
    repaint(); 
  } 
 
  public void paint(Graphics g) { 
     g.drawString(msg, 20, 100); 
  } 

  public static void main(String[] args) {
    ButtonList appwin = new ButtonList();

    appwin.setSize(new Dimension(250, 150));
    appwin.setTitle("ButtonList");
    appwin.setVisible(true);
  } 
}

listing 4
// Demonstrate check boxes.  
import java.awt.*; 
import java.awt.event.*; 
 
public class CheckboxDemo extends Frame implements ItemListener { 
  String msg = ""; 
  Checkbox windows, android, solaris, mac; 
 
  public CheckboxDemo() { 

    // Use a flow layout.
    setLayout(new FlowLayout());

    // Create check boxes.
    windows = new Checkbox("Windows", true); 
    android = new Checkbox("Android"); 
    solaris = new Checkbox("Solaris"); 
    mac = new Checkbox("Mac OS"); 

    // Add the check boxes to the frame.
    add(windows); 
    add(android); 
    add(solaris); 
    add(mac); 
 
    // Add item listeners.
    windows.addItemListener(this); 
    android.addItemListener(this); 
    solaris.addItemListener(this); 
    mac.addItemListener(this); 

    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
        System.exit(0);
      }
    });
  } 
 
  public void itemStateChanged(ItemEvent ie) { 
    repaint(); 
  } 
 
  // Display current state of the check boxes. 
  public void paint(Graphics g) { 
    msg = "Current state: "; 
    g.drawString(msg, 20, 120); 
    msg = "   Windows: " + windows.getState(); 
    g.drawString(msg, 20, 140); 
    msg = "   Android: " + android.getState(); 
    g.drawString(msg, 20, 160); 
    msg = "   Solaris: " + solaris.getState(); 
    g.drawString(msg, 20, 180); 
    msg = "   Mac OS: " + mac.getState(); 
    g.drawString(msg, 20, 200); 
  } 

  public static void main(String[] args) {
    CheckboxDemo appwin = new CheckboxDemo();

    appwin.setSize(new Dimension(240, 220));
    appwin.setTitle("CheckboxDemo");
    appwin.setVisible(true);
  } 
}

listing 5
// Demonstrate check box group.  
import java.awt.*; 
import java.awt.event.*; 
 
public class CBGroup extends Frame implements ItemListener { 
  String msg = ""; 
  Checkbox windows, android, solaris, mac; 
  CheckboxGroup cbg; 
 
  public CBGroup() { 

    // Use a flow layout.
    setLayout(new FlowLayout());

    // Create a check box group.
    cbg = new CheckboxGroup(); 

    // Create the check boxes and include them
    // in the group.
    windows = new Checkbox("Windows", cbg, true); 
    android = new Checkbox("Android", cbg, false); 
    solaris = new Checkbox("Solaris", cbg, false); 
    mac = new Checkbox("Mac OS", cbg, false); 
 
   // Add the check boxes to the frame.
    add(windows); 
    add(android); 
    add(solaris); 
    add(mac); 
 
    // Add item listeners.
    windows.addItemListener(this); 
    android.addItemListener(this); 
    solaris.addItemListener(this); 
    mac.addItemListener(this); 

    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
        System.exit(0);
      }
    });
  } 
 
  public void itemStateChanged(ItemEvent ie) { 
    repaint(); 
  } 
 
  // Display current state of the check boxes. 
  public void paint(Graphics g) { 
    msg = "Current selection: "; 
    msg += cbg.getSelectedCheckbox().getLabel(); 
    g.drawString(msg, 20, 120); 
  } 

  public static void main(String[] args) {
    CBGroup appwin = new CBGroup();

    appwin.setSize(new Dimension(240, 180));
    appwin.setTitle("CBGroup");
    appwin.setVisible(true);
  } 
}

listing 6
// Demonstrate Choice lists.  
import java.awt.*; 
import java.awt.event.*; 
 
public class ChoiceDemo extends Frame implements ItemListener { 
  Choice os, browser; 
  String msg = ""; 
 
  public ChoiceDemo() { 

    // Use a flow layout.
    setLayout(new FlowLayout());

    // Create choice lists.
    os = new Choice(); 
    browser = new Choice(); 
 
    // Add items to os list.
    os.add("Windows"); 
    os.add("Android"); 
    os.add("Solaris"); 
    os.add("Mac OS"); 
 
    // Add items to browser list.
    browser.add("Internet Explorer"); 
    browser.add("Firefox"); 
    browser.add("Chrome"); 
 
    // Add choice lists to window.
    add(os); 
    add(browser); 
 
    // Add item listeners. 
    os.addItemListener(this); 
    browser.addItemListener(this); 

    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
        System.exit(0);
      }
    });
  } 
 
  public void itemStateChanged(ItemEvent ie) { 
    repaint(); 
  } 
 
  // Display current selections. 
  public void paint(Graphics g) { 
    msg = "Current OS: "; 
    msg += os.getSelectedItem(); 
    g.drawString(msg, 20, 120); 
    msg = "Current Browser: "; 
    msg += browser.getSelectedItem(); 
    g.drawString(msg, 20, 140); 
  } 

  public static void main(String[] args) {
    ChoiceDemo appwin = new ChoiceDemo();

    appwin.setSize(new Dimension(240, 180));
    appwin.setTitle("ChoiceDemo");
    appwin.setVisible(true);
  } 
}

listing 7
// Demonstrate Lists.  
import java.awt.*; 
import java.awt.event.*; 
 
public class ListDemo extends Frame implements ActionListener { 
  List os, browser; 
  String msg = ""; 
 
  public ListDemo() { 

    // Use a flow layout.
    setLayout(new FlowLayout());

    // Create a multi-selection list.
    os = new List(4, true); 

    // Create a single-selection list.
    browser = new List(4); 
 
    // Add items to os list.
    os.add("Windows"); 
    os.add("Android"); 
    os.add("Solaris"); 
    os.add("Mac OS"); 
 
    // Add items to browser list.
    browser.add("Internet Explorer"); 
    browser.add("Firefox"); 
    browser.add("Chrome"); 
 
    // Make initial selections.
    browser.select(1); 
    os.select(0);
 
    // Add lists to the frame.
    add(os); 
    add(browser); 
 
    // Add action listeners. 
    os.addActionListener(this); 
    browser.addActionListener(this); 

    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
        System.exit(0);
      }
    });
  } 
 
  public void actionPerformed(ActionEvent ae) { 
    repaint(); 
  } 
 
  // Display current selections. 
  public void paint(Graphics g) { 
    int idx[]; 
 
    msg = "Current OS: "; 
    idx = os.getSelectedIndexes(); 
    for(int i=0; i<idx.length; i++) 
      msg += os.getItem(idx[i]) + "  "; 
    g.drawString(msg, 6, 120); 
    msg = "Current Browser: "; 
    msg += browser.getSelectedItem(); 
    g.drawString(msg, 6, 140); 
  } 

  public static void main(String[] args) {
    ListDemo appwin = new ListDemo();

    appwin.setSize(new Dimension(300, 180));
    appwin.setTitle("ListDemo");
    appwin.setVisible(true);
  } 
}

listing 8
// Demonstrate scroll bars.  
import java.awt.*; 
import java.awt.event.*; 
 
public class SBDemo extends Frame 
  implements AdjustmentListener { 

  String msg = ""; 
  Scrollbar vertSB, horzSB; 
 
  public SBDemo() { 

    // Use a flow layout.
    setLayout(new FlowLayout());

    // Create scroll bars and set preferred size.
    vertSB = new Scrollbar(Scrollbar.VERTICAL, 
                          0, 1, 0, 200); 
    vertSB.setPreferredSize(new Dimension(20, 100));

    horzSB = new Scrollbar(Scrollbar.HORIZONTAL, 
                           0, 1, 0, 100); 
    horzSB.setPreferredSize(new Dimension(100, 20));

    // Add the scroll bars to the frame.
    add(vertSB); 
    add(horzSB); 
 
    // Add AdjustmentListeners for the scroll bars.
    vertSB.addAdjustmentListener(this);
    horzSB.addAdjustmentListener(this);

    // Add MouseMotionListener.
    addMouseMotionListener(new MouseAdapter() {
      // Update scroll bars to reflect mouse dragging. 
      public void mouseDragged(MouseEvent me) { 
        int x = me.getX(); 
        int y = me.getY(); 
        vertSB.setValue(y); 
        horzSB.setValue(x); 
        repaint(); 
      } 
    }); 

    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
        System.exit(0);
      }
    });
  } 

  public void adjustmentValueChanged(AdjustmentEvent ae) {
    repaint();
  }

  // Display current value of scroll bars. 
  public void paint(Graphics g) { 
     msg = "Vertical: " + vertSB.getValue(); 
     msg += ",  Horizontal: " + horzSB.getValue(); 
     g.drawString(msg, 20, 160); 
 
     // show current mouse drag position 
     g.drawString("*", horzSB.getValue(), 
                  vertSB.getValue()); 
  } 

  public static void main(String[] args) {
    SBDemo appwin = new SBDemo();

    appwin.setSize(new Dimension(300, 180));
    appwin.setTitle("SBDemo");
    appwin.setVisible(true);
  } 
}

listing 9
// Demonstrate text field.  
import java.awt.*; 
import java.awt.event.*; 
 
public class TextFieldDemo extends Frame 
  implements ActionListener { 
 
  TextField name, pass; 
 
  public TextFieldDemo() { 

    // Use a flow layout.
    setLayout(new FlowLayout());

    // Create controls.
    Label namep = new Label("Name: ", Label.RIGHT); 
    Label passp = new Label("Password: ", Label.RIGHT); 
    name = new TextField(12); 
    pass = new TextField(8); 
    pass.setEchoChar('?'); 
 
    // Add the controls to the frame.
    add(namep); 
    add(name); 
    add(passp); 
    add(pass); 
 
    // Add action event handlers. 
    name.addActionListener(this); 
    pass.addActionListener(this); 

    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
        System.exit(0);
      }
    });
  } 
 
  // User pressed Enter. 
  public void actionPerformed(ActionEvent ae) { 
    repaint(); 
  } 
 
  public void paint(Graphics g) { 
     g.drawString("Name: " + name.getText(), 20, 100); 
     g.drawString("Selected text in name: " 
                  + name.getSelectedText(), 20, 120); 
     g.drawString("Password: " + pass.getText(), 20, 140); 
  } 
  
  public static void main(String[] args) {
    TextFieldDemo appwin = new TextFieldDemo();

    appwin.setSize(new Dimension(380, 180));
    appwin.setTitle("TextFieldDemo");
    appwin.setVisible(true);
  } 
}

listing 10
// Demonstrate TextArea. 
import java.awt.*; 
import java.awt.event.*;
 
public class TextAreaDemo extends Frame { 

  public TextAreaDemo() { 

    // Use a flow layout.
    setLayout(new FlowLayout());

    String val =  
      "Java 11 is the latest version of the most\n" + 
      "widely-used computer language for Internet programming.\n" + 
      "Building on a rich heritage, Java has advanced both\n" + 
      "the art and science of computer language design.\n\n" + 
      "One of the reasons for Java's ongoing success is its\n" + 
      "constant, steady rate of evolution. Java has never stood\n" + 
      "still. Instead, Java has consistently adapted to the\n" + 
      "rapidly changing landscape of the networked world.\n" + 
      "Moreover, Java has often led the way, charting the\n" + 
      "course for others to follow."; 
 
    TextArea text = new TextArea(val, 10, 30); 
    add(text); 

    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
        System.exit(0);
      }
    });
  }
 
  public static void main(String[] args) {
    TextAreaDemo appwin = new TextAreaDemo();

    appwin.setSize(new Dimension(300, 220));
    appwin.setTitle("TextAreaDemo");
    appwin.setVisible(true);
  } 
}

listing 11
// Demonstrate BorderLayout. 
import java.awt.*; 
import java.awt.event.*;
 
public class BorderLayoutDemo extends Frame { 
  public BorderLayoutDemo() { 

   // Here, BorderLayout is used by default.
 
    add(new Button("This is across the top."), 
        BorderLayout.NORTH); 
    add(new Label("The footer message might go here."), 
        BorderLayout.SOUTH); 
    add(new Button("Right"), BorderLayout.EAST); 
    add(new Button("Left"), BorderLayout.WEST); 
 
    String msg = "The reasonable man adapts " + 
      "himself to the world;\n" + 
      "the unreasonable one persists in " + 
      "trying to adapt the world to himself.\n" + 
      "Therefore all progress depends " + 
      "on the unreasonable man.\n\n" + 
      "        - George Bernard Shaw\n\n"; 
 
    add(new TextArea(msg), BorderLayout.CENTER); 

    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
        System.exit(0);
      }
    });
  } 

  public static void main(String[] args) {
    BorderLayoutDemo appwin = new BorderLayoutDemo();

    appwin.setSize(new Dimension(300, 220));
    appwin.setTitle("BorderLayoutDemo");
    appwin.setVisible(true);
  } 
}

listing 12
// Demonstrate BorderLayout with insets.  
import java.awt.*; 
import java.awt.event.*; 
 
public class InsetsDemo extends Frame { 

  public InsetsDemo() { 
    // Here, BorderLayout is used by default.

    // set background color so insets can be easily seen 
    setBackground(Color.cyan); 
 
    setLayout(new BorderLayout()); 
 
    add(new Button("This is across the top."), 
        BorderLayout.NORTH); 
    add(new Label("The footer message might go here."), 
        BorderLayout.SOUTH); 
    add(new Button("Right"), BorderLayout.EAST); 
    add(new Button("Left"), BorderLayout.WEST); 
 
    String msg = "The reasonable man adapts " + 
      "himself to the world;\n" + 
      "the unreasonable one persists in " + 
      "trying to adapt the world to himself.\n" + 
      "Therefore all progress depends " + 
      "on the unreasonable man.\n\n" + 
      "        - George Bernard Shaw\n\n"; 
 
    add(new TextArea(msg), BorderLayout.CENTER); 

    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
        System.exit(0);
      }
    });
  } 

  // Override getInsets to add inset values.
  public Insets getInsets() { 
    return new Insets(40, 20, 10, 20); 
  } 

  public static void main(String[] args) {
    InsetsDemo appwin = new InsetsDemo();

    appwin.setSize(new Dimension(300, 220));
    appwin.setTitle("InsetsDemo");
    appwin.setVisible(true);
  } 
}

listing 13
// Demonstrate GridLayout 
import java.awt.*; 
import java.awt.event.*;
 
public class GridLayoutDemo extends Frame { 
  static final int n = 4; 

  public GridLayoutDemo() { 

    // Use GridLayout.
    setLayout(new GridLayout(n, n)); 
 
    setFont(new Font("SansSerif", Font.BOLD, 24)); 
 
    for(int i = 0; i < n; i++) { 
      for(int j = 0; j < n; j++) { 
        int k = i * n + j; 
        if(k > 0) 
          add(new Button("" + k)); 
      } 
    } 

    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
        System.exit(0);
      }
    });
  } 

  public static void main(String[] args) {
    GridLayoutDemo appwin = new GridLayoutDemo();

    appwin.setSize(new Dimension(300, 220));
    appwin.setTitle("GridLayoutDemo");
    appwin.setVisible(true);
  } 
}

listing 14
// Demonstrate CardLayout.  
import java.awt.*; 
import java.awt.event.*; 
 
public class CardLayoutDemo extends Frame { 
 
  Checkbox windows10, windows7, windows8, android, solaris, mac; 
  Panel osCards; 
  CardLayout cardLO; 
  Button Win, Other; 
 
  public CardLayoutDemo() { 

    // Use a flow layout for the main frame.
    setLayout(new FlowLayout());

    Win = new Button("Windows"); 
    Other = new Button("Other"); 
    add(Win); 
    add(Other); 
 
    // Set osCarsds panel to use CardLayout.
    cardLO = new CardLayout(); 
    osCards = new Panel(); 
    osCards.setLayout(cardLO);  
 
    windows7 = new Checkbox("Windows 7",true); 
    windows8 = new Checkbox("Windows 8"); 
    windows10 = new Checkbox("Windows 10"); 
    android = new Checkbox("Android"); 
    solaris = new Checkbox("Solaris"); 
    mac = new Checkbox("Mac OS"); 
 
    // Add Windows check boxes to a panel.
    Panel winPan = new Panel(); 
    winPan.add(windows7); 
    winPan.add(windows8); 
    winPan.add(windows10); 
 
    // Add other OS check boxes to a panel.
    Panel otherPan = new Panel(); 
    otherPan.add(android); 
    otherPan.add(solaris); 
    otherPan.add(mac); 
 
    // Add panels to card deck panel.
    osCards.add(winPan, "Windows"); 
    osCards.add(otherPan, "Other"); 
 
    // Add cards to main frame panel.
    add(osCards); 
 
    // Use lambda expressions to handle button events.
    Win.addActionListener((ae) -> cardLO.show(osCards, "Windows")); 
    Other.addActionListener((ae) -> cardLO.show(osCards, "Other")); 
 
    // Register for mouse pressed events.
    addMouseListener(new MouseAdapter() {
      // Cycle through panels. 
      public void mousePressed(MouseEvent me) { 
        cardLO.next(osCards); 
      }
    });

    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
        System.exit(0);
      }
    }); 
  } 

  public static void main(String[] args) {
    CardLayoutDemo appwin = new CardLayoutDemo();

    appwin.setSize(new Dimension(300, 220));
    appwin.setTitle("CardLayoutDemo");
    appwin.setVisible(true);
  } 
}

listing 15
// Use GridBagLayout. 
import java.awt.*; 
import java.awt.event.*; 
 
public class GridBagDemo extends Frame 
  implements ItemListener { 
 
  String msg = ""; 
  Checkbox windows, android, solaris, mac; 
 
  public GridBagDemo() { 

    // Use a GridBagLayout
    GridBagLayout gbag = new GridBagLayout(); 
    GridBagConstraints gbc = new GridBagConstraints(); 
    setLayout(gbag); 
 
    // Define check boxes. 
    windows = new Checkbox("Windows ", true); 
    android = new Checkbox("Android"); 
    solaris = new Checkbox("Solaris"); 
    mac = new Checkbox("Mac OS"); 
 
    // Define the grid bag. 
 
    // Use default row weight of 0 for first row. 
    gbc.weightx = 1.0; // use a column weight of 1 
    gbc.ipadx = 200; // pad by 200 units 
    gbc.insets = new Insets(0, 6, 0, 0); // inset slightly from left 
 
    gbc.anchor = GridBagConstraints.NORTHEAST; 
 
    gbc.gridwidth = GridBagConstraints.RELATIVE; 
    gbag.setConstraints(windows, gbc); 
 
    gbc.gridwidth = GridBagConstraints.REMAINDER; 
    gbag.setConstraints(android, gbc); 
 
    // Give second row a weight of 1. 
    gbc.weighty = 1.0; 
 
    gbc.gridwidth = GridBagConstraints.RELATIVE; 
    gbag.setConstraints(solaris, gbc); 
 
    gbc.gridwidth = GridBagConstraints.REMAINDER; 
    gbag.setConstraints(mac, gbc); 
 
    // Add the components. 
    add(windows); 
    add(android); 
    add(solaris); 
    add(mac); 
 
    // Register to receive item events. 
    windows.addItemListener(this); 
    android.addItemListener(this); 
    solaris.addItemListener(this); 
    mac.addItemListener(this); 

    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
        System.exit(0);
      }
    });
  } 
 
  // Repaint when status of a check box changes. 
  public void itemStateChanged(ItemEvent ie) { 
    repaint(); 
  } 
 
  // Display current state of the check boxes. 
  public void paint(Graphics g) { 
    msg = "Current state: "; 
    g.drawString(msg, 20, 100); 
    msg = "  Windows: " + windows.getState(); 
    g.drawString(msg, 30, 120); 
    msg = "  Android: " + android.getState(); 
    g.drawString(msg, 30, 140); 
    msg = "  Solaris: " + solaris.getState(); 
    g.drawString(msg, 30, 160); 
    msg = "  Mac: " + mac.getState(); 
    g.drawString(msg, 30, 180); 
  } 

  public static void main(String[] args) {
    GridBagDemo appwin = new GridBagDemo();

    appwin.setSize(new Dimension(250, 200));
    appwin.setTitle("GridBagDemo");
    appwin.setVisible(true);
  } 
}

listing 16
// Illustrate menus.  
import java.awt.*; 
import java.awt.event.*; 
 
class MenuDemo extends Frame { 
  String msg = ""; 
  CheckboxMenuItem debug, test; 
 
  public MenuDemo() { 
 
    // Create menu bar and add it to frame.
    MenuBar mbar = new MenuBar(); 
    setMenuBar(mbar); 
 
    // Create the menu items.
    Menu file = new Menu("File"); 
    MenuItem item1, item2, item3, item4, item5; 
    file.add(item1 = new MenuItem("New...")); 
    file.add(item2 = new MenuItem("Open...")); 
    file.add(item3 = new MenuItem("Close")); 
    file.add(item4 = new MenuItem("-")); 
    file.add(item5 = new MenuItem("Quit...")); 
    mbar.add(file); 
 
    Menu edit = new Menu("Edit"); 
    MenuItem item6, item7, item8, item9; 
    edit.add(item6 = new MenuItem("Cut")); 
    edit.add(item7 = new MenuItem("Copy")); 
    edit.add(item8 = new MenuItem("Paste")); 
    edit.add(item9 = new MenuItem("-")); 
 
    Menu sub = new Menu("Special"); 
    MenuItem item10, item11, item12; 
    sub.add(item10 = new MenuItem("First")); 
    sub.add(item11 = new MenuItem("Second")); 
    sub.add(item12 = new MenuItem("Third")); 
    edit.add(sub); 
 
    // These are checkable menu items. 
    debug = new CheckboxMenuItem("Debug"); 
    edit.add(debug); 
    test = new CheckboxMenuItem("Testing"); 
    edit.add(test); 
 
    mbar.add(edit); 
 
    // Create an object to handle action and item events. 
    MyMenuHandler handler = new MyMenuHandler(); 

    // Register to receive those events. 
    item1.addActionListener(handler); 
    item2.addActionListener(handler); 
    item3.addActionListener(handler); 
    item4.addActionListener(handler); 
    item6.addActionListener(handler); 
    item7.addActionListener(handler); 
    item8.addActionListener(handler); 
    item9.addActionListener(handler); 
    item10.addActionListener(handler); 
    item11.addActionListener(handler); 
    item12.addActionListener(handler); 
    debug.addItemListener(handler); 
    test.addItemListener(handler); 
 
    // Use a lambda expression to handle the Quit selection.
    item5.addActionListener((ae) -> System.exit(0));

    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
        System.exit(0);
      }
    });
  } 
 
  public void paint(Graphics g) { 
    g.drawString(msg, 10, 220); 

   if(debug.getState()) 
      g.drawString("Debug is on.", 10, 240); 
    else 
      g.drawString("Debug is off.", 10, 240); 
 
    if(test.getState()) 
      g.drawString("Testing is on.", 10, 260); 
    else 
      g.drawString("Testing is off.", 10, 260); 
  } 

  public static void main(String[] args) {   
    MenuDemo appwin = new MenuDemo();

    appwin.setSize(new Dimension(250, 300));
    appwin.setTitle("MenuDemo");
    appwin.setVisible(true);
  } 

  // An inner class for handling action and item events
  // for the menu. 
  class MyMenuHandler implements ActionListener, ItemListener { 

    // Handle action events.
    public void actionPerformed(ActionEvent ae) { 
        msg = "You selected "; 
        String arg = ae.getActionCommand(); 

        if(arg.equals("New...")) 
          msg += "New."; 
        else if(arg.equals("Open...")) 
          msg += "Open."; 
        else if(arg.equals("Close")) 
          msg += "Close."; 
        else if(arg.equals("Edit")) 
          msg += "Edit."; 
        else if(arg.equals("Cut")) 
          msg += "Cut."; 
        else if(arg.equals("Copy")) 
          msg += "Copy."; 
        else if(arg.equals("Paste")) 
          msg += "Paste."; 
        else if(arg.equals("First")) 
          msg += "First."; 
        else if(arg.equals("Second")) 
          msg += "Second."; 
        else if(arg.equals("Third")) 
          msg += "Third."; 
        else if(arg.equals("Debug")) 
          msg += "Debug."; 
        else if(arg.equals("Testing")) 
          msg += "Testing."; 

        repaint(); 
    } 

    // Handle item events.
    public void itemStateChanged(ItemEvent ie) { 
      repaint(); 
    } 
  } 
} 

listing 17
// Illustrate a dialog box.  
import java.awt.*; 
import java.awt.event.*; 
 
class DialogDemo extends Frame { 
  String msg = ""; 
  CheckboxMenuItem debug, test; 
  SampleDialog myDialog;
 
  public DialogDemo() { 

    // Create the dialog box.
    myDialog  = new SampleDialog(this, "New Dialog Box");  

    // Create menu bar and add it to frame.
    MenuBar mbar = new MenuBar(); 
    setMenuBar(mbar); 
 
    // Create the menu items.
    Menu file = new Menu("File"); 
    MenuItem item1, item2, item3, item4, item5; 
    file.add(item1 = new MenuItem("New...")); 
    file.add(item2 = new MenuItem("Open...")); 
    file.add(item3 = new MenuItem("Close")); 
    file.add(item4 = new MenuItem("-")); 
    file.add(item5 = new MenuItem("Quit...")); 
    mbar.add(file); 
 
    Menu edit = new Menu("Edit"); 
    MenuItem item6, item7, item8, item9; 
    edit.add(item6 = new MenuItem("Cut")); 
    edit.add(item7 = new MenuItem("Copy")); 
    edit.add(item8 = new MenuItem("Paste")); 
    edit.add(item9 = new MenuItem("-")); 
 
    Menu sub = new Menu("Special"); 
    MenuItem item10, item11, item12; 
    sub.add(item10 = new MenuItem("First")); 
    sub.add(item11 = new MenuItem("Second")); 
    sub.add(item12 = new MenuItem("Third")); 
    edit.add(sub); 
 
    // These are checkable menu items. 
    debug = new CheckboxMenuItem("Debug"); 
    edit.add(debug); 
    test = new CheckboxMenuItem("Testing"); 
    edit.add(test); 
 
    mbar.add(edit); 
 
    // Create an object to handle action and item events. 
    MyMenuHandler handler = new MyMenuHandler(); 

    // Register to receive those events. 
    item1.addActionListener(handler); 
    item2.addActionListener(handler); 
    item3.addActionListener(handler); 
    item4.addActionListener(handler); 
    item6.addActionListener(handler); 
    item7.addActionListener(handler); 
    item8.addActionListener(handler); 
    item9.addActionListener(handler); 
    item10.addActionListener(handler); 
    item11.addActionListener(handler); 
    item12.addActionListener(handler); 
    debug.addItemListener(handler); 
    test.addItemListener(handler); 
 
    // Use a lambda expression to handle the Quit selection.
    item5.addActionListener((ae) -> System.exit(0));

    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
        System.exit(0);
      }
    });
  } 
 
  public void paint(Graphics g) { 
    g.drawString(msg, 10, 220); 

   if(debug.getState()) 
      g.drawString("Debug is on.", 10, 240); 
    else 
      g.drawString("Debug is off.", 10, 240); 
 
    if(test.getState()) 
      g.drawString("Testing is on.", 10, 260); 
    else 
      g.drawString("Testing is off.", 10, 260); 
  } 

  public static void main(String[] args) {   
    DialogDemo appwin = new DialogDemo();

    appwin.setSize(new Dimension(250, 300));
    appwin.setTitle("DialogDemo");
    appwin.setVisible(true);
  } 

  // An inner class for handling action and item events
  // for the menu. 
  class MyMenuHandler implements ActionListener, ItemListener { 

    // Handle action events.
    public void actionPerformed(ActionEvent ae) { 
        msg = "You selected "; 
        String arg = ae.getActionCommand(); 

       if(arg.equals("New...")) { 
          msg += "New."; 
          myDialog.setVisible(true); 
        } 
        else if(arg.equals("Open...")) 
          msg += "Open."; 
        else if(arg.equals("Close")) 
          msg += "Close."; 
        else if(arg.equals("Edit")) 
          msg += "Edit."; 
        else if(arg.equals("Cut")) 
          msg += "Cut."; 
        else if(arg.equals("Copy")) 
          msg += "Copy."; 
        else if(arg.equals("Paste")) 
          msg += "Paste."; 
        else if(arg.equals("First")) 
          msg += "First."; 
        else if(arg.equals("Second")) 
          msg += "Second."; 
        else if(arg.equals("Third")) 
          msg += "Third."; 
        else if(arg.equals("Debug")) 
          msg += "Debug."; 
        else if(arg.equals("Testing")) 
          msg += "Testing."; 

        repaint(); 
    } 

    // Handle item events.
    public void itemStateChanged(ItemEvent ie) { 
      repaint(); 
    } 
  } 
} 
 
// Create a subclass of Dialog. 
class SampleDialog extends Dialog { 
  SampleDialog(Frame parent, String title) { 
    super(parent, title, false); 
    setLayout(new FlowLayout()); 
    setSize(300, 200); 
 
    add(new Label("Press this button:")); 

    Button b; 
    add(b = new Button("Cancel")); 
    b.addActionListener((ae) -> dispose()); 

    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
        dispose();
      }
    });
  } 

  public void paint(Graphics g) { 
    g.drawString("This is in the dialog box", 20, 80); 
  } 
} 

