Java Reference
In-Depth Information
Figure 6-9 displays the addActionListener() and setActionCommand()
methods for each of the commands on the menus. Because commands on the
menu bar itself have no other action except to trigger the display of each menu,
they do not need an ActionListener. The capability to display their related menu
list is polymorphic.
56
// add the ActionListener to each menu item
57
mnuFileExit.addActionListener ( this ) ;
58
mnuEditClear.addActionListener ( this ) ;
59
mnuEditCopy.addActionListener ( this ) ;
60
mnuEditPaste.addActionListener ( this ) ;
61
mnuAboutCalculator.addActionListener ( this ) ;
62
63
// assign an ActionCommand to each menu item
64
mnuFileExit.setActionCommand ( "Exit" ) ;
65
mnuEditClear.setActionCommand ( "Clear" ) ;
66
mnuEditCopy.setActionCommand ( "Copy" ) ;
67
mnuEditPaste.setActionCommand ( "Paste" ) ;
68
mnuAboutCalculator.setActionCommand ( "About" ) ;
69
FIGURE 6-9
The following steps enter code to create and implement the menu system for
the Calculator application.
To Enter Code to Create the Menu System
1. Enter lines 27 through 55 as shown in Figure 6-8.
The TextPad window displays the constructor method header, along with the
code to construct the menu components (Figure 6-10).
constructor
method header
code to create
menu bar
code to create
File menu
code to create
Edit menu
code to
create
About menu
FIGURE 6-10
(continued)
 
Search WWH ::




Custom Search