Chapter contents ] [ Course contents ] [ Write to us! webmaster@keller.com ] [ Home ] [ Curriculum ]

Chapter 10

Menus

Food for thought: How can we add a menu bar to our Frame?
import accounting.*;
import java.awt.*;

class Accounting extends Frame {

  StockAccount account;
  static final String NAME = "Accounting";

  public Accounting() {
    super(NAME);
    account = new StockAccount(NAME);
    try {
      account.readFromFile();
    } catch (Exception e) {
      System.err.println(e + " could not read from file");
    }
    setLocation(new Point(30, 30));
    DisplayCanvas canvas = new DisplayCanvas(account);
    add(canvas);
    Menu file = new Menu("File", true);
    MenuItem quit = new MenuItem("Quit");
    file.add(quit);
    MenuBar mb = new MenuBar();
    mb.add(file);
    setMenuBar(mb);
    pack();
  }

  public static void main(String[] args) {   
    Accounting a = new Accounting();
    a.setVisible(true);
  }
}

Note: We examine how to handle events caused by clicking on a menu item in the next lesson.

Review:


Chapter contents ] [ Course contents ] [   ] [ Home ] [ Curriculum ]
http://www.keller.com/java/chapter_10/doc/menu.html
Updated Wednesday, 31-Oct-2001 14:19:00 CST

Copyright © 2001
Dan Keller Technical Services
4500 19th St., San Francisco
California, USA 94114
tel: 415 / 861-4500