Java Reference
In-Depth Information
JMenu m1 = new JMenu(“File”);
m1.add(j1);
m1.add(j2);
m1.add(j3);
m1.addSeparator();
m1.add(j4);
m1.add(j5);
m1.addSeparator();
m1.add(j6);
m1.addSeparator();
m1.add(j7);
A JMenuBar container holds one or more JMenu containers and displays each of their
names. The most common place to see a menu bar is directly below an application's title
bar.
To create a menu bar, call the JMenuBar() constructor method with no arguments. Add
menus to the end of a bar by calling its add( JMenu) method.
10
After you have created all your items, added them to menus, and added the menus to a
bar, you're ready to add them to a frame. Call the frame's setJMenuBar( JMenuBar)
method.
The following statement finishes off the current example by creating a menu bar, adding
a menu to it, and then placing the bar on a frame called gui :
JMenuBar bar = new JMenuBar();
bar.add(m7);
gui.setJMenuBar(bar);
Figure 10.14 shows what this menu looks like on an otherwise empty menu bar.
FIGURE 10.14
A frame with a
menu bar.
Although you can open and close a menu and select items, nothing happens in response.
You'll learn how to receive user input for this component and others during Day 12,
“Responding to User Input.”
Search WWH ::




Custom Search