There are four different types of MenuComponent, all derived from that abstract class: MenuBar (you can have one MenuBar only on a particular Frame), Menu to hold one individual drop-down menu or submenu, MenuItem to represent one single element on a menu, and CheckboxMenuItem, which is derived from MenuItem and produces a checkmark to indicate whether that menu item is selected.
Unlike a system that uses resources, with Java and the AWT you must hand assemble all the menus in source code. Here are the ice cream flavors again, used to create menus:
Case Study: Menu1.java
In this program I avoided the typical long lists of add( ) calls for each menu because that seemed like a lot of unnecessary typing. Instead, I placed the menu items into arrays and then simply stepped through each array calling add( ) in a for loop. This makes adding or subtracting a menu item less tedious.
As an alternative approach (which I find less desirable since it requires more typing), the CheckboxMenuItems are created in an array of handles called safety; this is true for the arrays file and other as well.
This program creates not one but two MenuBars to demonstrate that menu bars can be actively swapped while the program is running. You can see how a MenuBar is made up of Menus, and each Menu is made up of MenuItems, CheckboxMenuItems, or even other Menus (which produce submenus). When a MenuBar is assembled it can be installed into the current program with the setMenuBar( ) method. Note that when the button is pressed, it checks to see which menu is currently installed using getMenuBar( ), then puts the other menu bar in its place.
When testing for "Open",” notice that spelling and capitalization are critical, but Java signals no error if there is no match with "Open".” This kind of string comparison is a clear source of programming errors.
The checking and un-checking of the menu items is taken care of automatically, but dealing with CheckboxMenuItems can be a bit surprising since for some reason they don't allow string matching. (Although string matching isn't a good approach, this seems inconsistent.) So you can match only the target object and not its label. As shown, the getState( ) method can be used to reveal the state. You can also change the state of a CheckboxMenuItem with setState( ).
You might think that one menu could reasonably reside on more than one menu bar. This does seem to make sense because all you're passing to the MenuBar add( ) method is a handle. However, if you try this, the behavior will be strange and not what you expect. (It's difficult to know if this is a bug or if they intended it to work this way.) This example also shows what you need to do to create an application instead of an applet. (Again, because an application can support menus and an applet cannot directly have a menu.) Instead of inheriting from Applet, you inherit from Frame. Instead of init( ) to set things up, you make a constructor for your class. Finally, you create a main( ) and in that you build an object of your new type, resize it, and then call show( ). It's different from an applet in only a few small places, but it's now a standalone windowed application and you've got menus.