11.6 Creating a Menu 

Previous Index Next 


A Frame  can have a menu. To create a menu three different classes are used:  The following code creates a menu bar::
MenuBar menuBar = new MenuBar();
Menu fileMenu = new Menu("File");
MenuItem quitItem = new MenuItem("Quit");
 
menuBar.add(fileMenu);
fileMenu.add(quitItem);
 
quitItem.addActionListener( new QuitActionListener() );
To add the menu bar to a Frame you would call the setMenuBar()  method on the frame. For example
setMenuBar(menuBar);
Notice how the quitItem object is associated with an ActionListener. The code on the ActionListener will be triggered when the Quit option is selected from the file menu.


1