A Frame has a set of classes to define MenuBars and Menus:
-
Menubar mbar = new MenuBar(); // defines a menubar which can be used in a frame
|
Several Menus, which each have menu items, can be added to the menubar:
|
Menu typeMenu = new Menu("Type");
|
typeMenu.add(new MenuItem("Black/White"));
|
typeMenu.add(new MenuItem("Color"));
|
typeMenu.addSeparator();
|
MenuItem q = new MenuItem("Quit"); q.addActionListener;
-
typeMenu.add ( q );
-
mbar.add(typeMenu);
-
When the user selects an item on a menu, an ActionEvent is reported to the ActionListener, where it can be handled
-
public void actionPerformed(ActionEvent e)
-
{ if (e.getSource() == q)
-
{ q.setVisible(false); } }
|
Note that there are also CheckboxMenuItems, submenus, and (new) MenuShortcuts.
|