A Frame can have a menu. To create a menu three different
classes are used:
-
MenuBar - This represents the actual menu bar. It holds all the other menu
objects.
-
Menu - This represents a drop down menu.
-
MenuItem - This represents an option on the drop down menu.
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.