[2014] in java-interest

home help back first fref pref prev next nref lref last post

Re> Simple awt Menu prgram

daemon@ATHENA.MIT.EDU (Sid Conklin)
Thu Sep 21 19:59:03 1995

Date: 21 Sep 1995 13:59:00 -0700
From: "Sid Conklin" <sid.conklin@nora.stanford.edu>
To: "java-interest@java.sun.com" <java-interest@java.sun.com>

Sami's code had a few missing items: not adding the menu to the menubar; and
not casting the evt.target to a menuItem. Here is the modified code:

import java.awt.*;

public class MenuTest extends Frame {
  public MenuTest() {
	super("MenuTest"); // set the title of the frame
	MenuBar mb = new MenuBar();

	// add a simple menu
	Menu m = new Menu("Numbers");
	m.add(new MenuItem("one"));
	m.add(new MenuItem("two"));
	m.add(new MenuItem("three"));

	// add a submenu
	Menu submenu = new Menu("More Numbers");
	submenu.add(new MenuItem("four"));
	submenu.add(new MenuItem("five"));
	m.add(submenu);
	
	//add menu to menubar
	mb.add(m);
	
	setMenuBar(mb); // install this menu bar in the frame

	resize(300, 300);
	show();
  }
  public boolean handleEvent(Event evt) {

	if ((evt.id == Event.ACTION_EVENT) && (evt.target instanceof MenuItem)){
		System.out.println(((MenuItem)evt.target).getLabel());
		return true;
	}
	return false;
  }
  public static void main(String args[]) {
	new MenuTest();
  }
}


Good luck,

Sid
Stanford University

 ------ From: java-interest@java.sun.com, Thu, Sep 21, 1995 ------ 

Here's a simple awt program that puts up a menubar and prints out the
contents of a menu item when selected. Compile it with javac and then
just run it as "java MenuTest":

import java.awt.*;

public class MenuTest extends Frame {
  public MenuTest() {
	super("MenuTest"); // set the title of the frame
	MenuBar mb = new MenuBar();

	// add a simple menu
	Menu m = new Menu("Numbers");
	m.add(new MenuItem("one"));
	m.add(new MenuItem("two"));
	m.add(new MenuItem("three"));

	// add a submenu
	Menu submenu = new Menu("More Numbers");
	submenu.add(new MenuItem("four"));
	submenu.add(new MenuItem("five"));
	m.add(submenu);
	
	setMenuBar(mb); // install this menu bar in the frame

	resize(300, 300);
	show();
  }
  public boolean handleEvent(Event evt) {

	if ((evt.id == Event.ACTION_EVENT) && (evt.target instanceof MenuItem)){
		System.out.println(evt.target.getLabel());
	}
  }
  public static void main(String args[]) {
	new MenuTest();
  }
}


-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post