A Menu Example

An application may have dozens, even hundreds of menu items. Cramming these all into the init() method gets confusing. It's customary to create separate methods that create each individual menu and add it to the MenuBar. This program creates two fairly standard menus, File and Edit.

import java.applet.*;
import java.awt.*;


public class MenuTester extends Applet {

  public void init () {
  
    Frame f = new Frame("Simple Window");
    f.add("Center", new Label("Look at the Menus", Label.CENTER));
    f.setSize(this.getSize().width, this.getSize().height);
    f.setLocation(320,240);
    MenuBar myMenuBar = new MenuBar();
    this.makeFileMenu(myMenuBar);
    this.makeEditMenu(myMenuBar);
    f.setMenuBar(myMenuBar);
    f.show();

  }
  
  void makeEditMenu(MenuBar mb) {
  
    Menu editMenu = new Menu("Edit");
    editMenu.add("Undo");
    editMenu.addSeparator();
    editMenu.add("Cut");
    editMenu.add("Copy");
    editMenu.add("Paste");
    editMenu.add("Clear");
    mb.add(editMenu);
    
  }

  void makeFileMenu(MenuBar mb) {
  
    Menu fileMenu = new Menu("File");
    fileMenu.add("New");
    fileMenu.add("Open...");
    fileMenu.addSeparator();
    fileMenu.add("Close");
    fileMenu.add("Save");
    fileMenu.add("Save As...");
    fileMenu.addSeparator();
    fileMenu.add("Page Setup...");
    fileMenu.add("Print");
    fileMenu.addSeparator();
    fileMenu.add("Quit");
    mb.add(fileMenu);
    
  }

}

Previous | Next | Top
Last Modified July 8, 1998
Copyright 1997, 1998 Elliotte Rusty Harold
elharo@metalab.unc.edu