Java Reference
In-Depth Information
The setMenuBar() method assigns the MenuBar to the Frame. The add()
method is used in a similar manner to the add() methods used for applets;
it adds the object specified in the argument to the container. The
addActionListener() method makes the component clickable, as it does with
Buttons. Finally, the setActionCommand() method takes a String argument
that becomes a reference that can be checked with an if statement. The
remove() method deletes an item from the menu. The insertSeparator()
method inserts a horizontal separator line at the index position. For example,
using the insertSeparator() method with an index of one would draw a
horizontal separator line after the first item in the menu.
Using Menus in the Calculator Application
The Calculator application will contain three commands on the menu bar:
File, Edit, and About, as shown in Figure 6-1 on page 380. The File menu will
contain only the Exit command. The Edit menu will contain three commands:
Clear, Copy, and Paste. A horizontal separator line will display between the Clear
and Copy commands on the Edit menu. The About menu will contain only the
About Calculator command.
When assigning menu variable identifiers, it is common practice to use the
prefix, mnu, for each item and then to list the menu bar command before any
menu commands in the variable name. For example, the identifier, mnuFile, would
be used to name the File command on the menu bar; the identifier, mnuFileExit,
would be used to name the Exit command on the File menu; and so forth.
Figure 6-8 displays the beginning of the Calculator() constructor method
with its header in line 27. Lines 28 through 54 include the code to set up the
menu system for the Calculator application. Line 30 constructs a menu bar
named mnuBar, and line 31 sets the menu bar to the Frame by default because
no object precedes the method call. Lines 34 through 37 create and populate the
File menu. Lines 40 through 48 create and populate the Edit menu, with its three
commands and separator line (line 44). Lines 51 through 54 construct and
populate the About menu.
FIGURE 6-8
27
public Calculator ()
28
{
29
// create an instance of the menu
30
MenuBar mnuBar = new MenuBar () ;
31
setMenuBar ( mnuBar ) ;
32
33
// construct and populate the File menu
34
Menu mnuFile = new Menu ( "File" , true ) ;
35
mnuBar.add ( mnuFile ) ;
36
MenuItem mnuFileExit = new MenuItem ( "Exit" ) ;
37
mnuFile.add ( mnuFileExit ) ;
38
39
// construct and populate the Edit menu
40
Menu mnuEdit = new Menu ( "Edit" , true ) ;
41
mnuBar.add ( mnuEdit ) ;
42
MenuItem mnuEditClear = new MenuItem ( "Clear" ) ;
43
mnuEdit.add ( mnuEditClear ) ;
44
mnuEdit.insertSeparator ( 1 ) ;
45
MenuItem mnuEditCopy = new MenuItem ( "Copy" ) ;
46
mnuEdit.add ( mnuEditCopy ) ;
47
MenuItem mnuEditPaste = new MenuItem ( "Paste" ) ;
48
mnuEdit.add ( mnuEditPaste ) ;
49
50
// construct and populate the About menu
51
Menu mnuAbout = new Menu ( "About" , true ) ;
52
mnuBar.add ( mnuAbout ) ;
53
MenuItem mnuAboutCalculator = new MenuItem ( "About Calculator" ) ;
54
mnuAbout.add ( mnuAboutCalculator ) ;
55
Search WWH ::




Custom Search