Menu
Home Up Button Checkbox CheckboxGroup Choice Key List Menu Mouse Popup Window Text

 

The code below utilizes a menubar.  Experiment with the applet below to see what it does.  Then proceed to understand the code that generates the applet.

You would see an applet here if your browser supported Java.

The "init" method begins by creating an instance of "MyWindow".   (Menubars can not be added to the display window of an applet.)   This instance is then sized to be 500 pixels wide and 500 pixels high.  Its background color is set to white, its foreground color is set to black, the layout manager is set to the flow layout manager, and it is then displayed with the "setVisible(true)" command.

Notice that "MyWindow" extends the built-in class "Frame".   The constructor for this class adds a menubar across the top of the frame.   This is a somewhat laborious process and works as follows.  First, a new "MenuBar" is created.  Then, one or more "Menu"s can be added to the "MenuBar".  In the example below, a single "Menu" (named "colorMenu") is added to the "MenuBar".

To build up a "Menu", one can add either "MenuItem"s or separators to it.  A "MenuItem" is a choice that a user can select from the "Menu".  A separator is a line that can be used to group logically related choices in the "Menu" together.

Notice that there are three things that must be done to use a "MenuItem".   First, an instance must be instantiated.  Second, an action listener must be added for the individual "MenuItem" so that activity with that instance (such as selecting it) can be detected.  Third, the "MenuItem" must be added to the "Menu".

After the "Menu" is complete, the "Menu" is added to the "MenuBar".  When the "MenuBar" is complete, it is added to the "Frame" via the "setMenuBar" method at the very bottom of the constructor.  Make sure that you understand how the "MenuBar" has been constructed before proceeding.

The final method in the "MyWindow" class is "actionPerformed".   Since the class implements "ActionListener", this method must be defined according to the event handling summary.  The method begins by calling the "getActionCommand" method.  This method returns the string label of the menu item selected.  If the person chose "red", "white", or "blue", the background is set accordingly.  If the person chose the "exit" option, a call to "setVisible(false);" is made to hide the frame. 

import java.awt.*;
import java.awt.event.*;
import java.applet.*;          
public class Example extends Applet
{
	public void init()
	{
		MyWindow w = new MyWindow();          
		w.setSize(500, 500);
		w.setBackground(Color.white);
		w.setForeground(Color.black);
		w.setLayout(new FlowLayout());          
		w.setVisible(true);
	}
}          
class MyWindow extends Frame implements ActionListener
{
	MyWindow ()
	{
		MenuBar mainMenuBar = new MenuBar();          
		Menu colorMenu = new Menu("Color");          
		MenuItem redItem = new MenuItem("red");
		redItem.addActionListener(this);
		colorMenu.add(redItem);          
		MenuItem whiteItem = new MenuItem("white");
		whiteItem.addActionListener(this);
		colorMenu.add(whiteItem);          
		MenuItem blueItem = new MenuItem("blue");
		blueItem.addActionListener(this);
		colorMenu.add(blueItem);          
		colorMenu.addSeparator();          
		MenuItem exitItem = new MenuItem("exit");
		exitItem.addActionListener(this);
		colorMenu.add(exitItem);          
		mainMenuBar.add(colorMenu);          
		setMenuBar(mainMenuBar);          
	}          
	public void actionPerformed (ActionEvent evt)
	{
		String arg = evt.getActionCommand();          
		if (arg.equals("red"))
			setBackground(Color.red);
		else if (arg.equals("white"))
			setBackground(Color.white);
		else if (arg.equals("blue"))
			setBackground(Color.blue);
		else if (arg.equals("exit"))
			setVisible(false);          
		repaint();
	}          
}