Java Reference
In-Depth Information
17.4
Menus and Buttons
For hours and location press 1.
For a recorded message describing services press 2.
For instructions on using our website press 3.
To use our automated information system press 4.
To speak to an operator between 8 am and noon Monday through
Thursdays press 7.
PHONE ANSWERING MACHINE
In this section, we describe the basics of Swing menus. Swing menu items (menu
choices) behave essentially the same as Swing buttons. They generate action events that
are handled by action listeners, just as buttons do.
EXAMPLE: A GUI with a Menu
Display 17.14 contains a program that is essentially the same as the GUI in
Display 17.11 except that this GUI uses a menu instead of buttons. This GUI
has a menu bar at the top of the window. The menu bar lists the names of all the
pull-down menus. This GUI has only one pull-down menu, which is named "Add
Colors" . However, there could be more pull-down menus in the same menu bar.
The user can pull down a menu by clicking its name in the menu bar. Display 17.14
contains three pictures of the GUI. The first is what you see when the GUI originally
appears. In that picture, the menu name "Add Colors" can be seen in the menu
bar, but you cannot see the menu. If you click the words "Add Colors" with your
mouse, the menu drops down, as shown in the second picture of the GUI. If you
click "Green" , "White" , or "Gray" on the menu, then a vertical strip of the named
color appears in the GUI.
In the next subsection, we go over the details of the program in Display 17.14.
Menu Bars, Menus, and Menu Items
When adding menus as we did in Display 17.14, use the three Swing classes JMenuItem ,
JMenu , and JMenuBar . Entries on a menu are objects of the class JMenuItem.
These JMenuItem s are placed in JMenu s, and then the JMenu s are typically placed in a
JMenuBar . Let us look at the details.
A menu is an object of the class JMenu . A choice on a menu is called a menu item
and is an object of the class JMenuItem. A menu item is identified by the string that
labels it, such as "Blue" , "White" , or "Gray" in the menu in Display 17.14. You can
add as many JMenuItem s as you wish to a menu. The menu lists the items in the order
menu
menu item
 
 
Search WWH ::




Custom Search