It is not possible to place a menu directly in the program (Java 1.0,java1.1 and swing libraries are not allowed) because they are for the application. Go on, if you don't believe me and make sure you can reasonably have the menu in the program, you can try it. There is no Setmenubar () method in the patch, and this method is attached to the menu (we will see that it can reasonably produce a frame in the program, and the frame contains the menu). The
has four different types of menucomponent (menu components), all menu components originate from abstract Classes: Menu bar (we can have a menu bar in an event frame), menu to control a separate Drop-down menu or submenu, menu items to describe a single element of the menus, As well as originating from MenuItem, a check mark (checkmark) is generated to show whether the menu item is selected Checkboxmenuitem.
Different systems use different resources, and for Java and AWT, we have to manually assemble all the menus in the source code.
: Menu1.java//Menus work only with Frames.
Shows submenus, checkbox menu items//and swapping menus.
Import java.awt.*; public class Menu1 extends Frame {string[] flavors = {"Chocolate", "Strawberry", "Vanilla Fudge, Swirl", "Mint Chip
"," Mocha Almond Fudge "," Rum Raisin "," Praline Cream "," Mud Pie "};
TextField t = new TextField ("No flavor", 30);
MenuBar MB1 = new MenuBar ();
Menu F = new menu ("File");
Menu m = new menu ("flavors");
Menu s = new menu ("Safety"); Alternative approach:checkboxmenuitem[] safety = {New Checkboxmenuitem ("Guard"), New Checkboxmenuitem ("Hide
")
};
menuitem[] File = {new MenuItem ("Open"), New MenuItem ("Exit")};
A second menu bar to swap to:menubar MB2 = new MenuBar ();
Menu FooBar = new Menu ("FooBar");
Menuitem[] Other = {new MenuItem ("Foo"), New MenuItem ("Bar"), New MenuItem ("Baz"),};
Button B = New button ("Swap Menus"); Public Menu1 () {for (int i = 0; i < Flavors.length;
i++) {M.add (new MenuItem (Flavors[i]));
ADD separators at Intervals:if ((i+1)% 3 = 0) m.addseparator ();
for (int i = 0; i < safety.length i++) S.add (safety[i));
F.add (s);
for (int i = 0; i < file.length i++) F.add (file[i));
Mb1.add (f);
Mb1.add (m);
Setmenubar (MB1);
T.seteditable (FALSE);
Add ("Center", t);
Set up the system for swapping Menus:add ("North", b);
for (int i = 0; i < other.length i++) Foobar.add (other[i));
Mb2.add (FooBar);
public boolean handleevent (Event evt) {if (evt.id = = Event.window_destroy) system.exit (0);
else return super.handleevent (EVT);
return true;
Public boolean action (Event evt, Object Arg) {if (evt.target.equals (b)) {MenuBar m = GetMenuBar ();
if (m = = MB1) Setmenubar (MB2);
else if (m = = MB2) Setmenubar (MB1); else if (evt.target instanceof MenuItem) {if (ARG.equals ("Open")) {String s = t.gettext ();
Boolean chosen = false;
for (int i = 0; i < flavors.length i++) if (S.equals (flavors[i)) chosen = true;
if (!chosen) T.settext ("Choose a Flavor first!"); else T.settext ("Opening" + S +.)
MMM, mm! ");}
else if (Evt.target.equals (file[1])) system.exit (0); Checkboxmenuitems cannot use String//matching;
You must match the Target:else if (Evt.target.equals (safety[0)) T.settext ("Guard the Ice cream!" +
"Guarding is" + safety[0].getstate ()); else if (evt.target.equals (safety[1)) T.settext ("Hide the Ice cream!" + "Is it cold?" + Safety[1].gets
Tate ());
Else T.settext (arg.tostring ());
else return super.action (EVT, ARG);
return true;
public static void Main (string[] args) {Menu1 f = new Menu1 ();
F.resize (300,200);
F.show (); }
} ///:~
In this program, I avoided writing a typical lengthy add () List call for each menu because it looked like a lot of useless flags. Instead, I put the menu item into the array and then simply skip it by calling Add () through each array in a for loop. In this way, adding and reducing menu items becomes less annoying.
As an alternative method (I find it hard to satisfy because it requires more allocations) Checkboxmenuitems is created in the handle of an array is called security creation; This is true security for the pairs of files and other files.
A menu bar that is not one but two is created in the program to prove that the menu bar can be switched on when the program is running. We can see how the menu bar makes up the menu, how each menu is made up of menu items (MenuItems), chenkboxmenuitems, or other menus (produces submenus). When the menu is grouped, you can install it into your current program using the Setmenubar () method. It is worth noting that when the button is pressed, it checks the current menu installation using GetMenuBar (), and then places the other menu bar in its place.
When the test is "open" (that is, start), pay attention to spelling and capitalization, and if there is no object at the beginning, Java emits a signal of no error (no errors). This string comparison is an obvious source of program design errors.
Check-and-Verify menu items run automatically, and the checkboxmenuitems associated with them are surprisingly surprising, for some reason they do not allow string matching. (This seems paradoxical, although string matching is not a good idea.) Therefore, we can match a target object rather than their label. When demonstrating, the GetState () method is used to display the state. We can also use SetState () to change the state of Checkboxmenuitem.
We might think that a menu can reasonably be placed in more than one menu bar. This seems reasonable, because all the add () Methods of the menu bar that we ignore are all a handle. However, if we try to do this, the result will be very awkward and far from what we want. (It's hard to know if this is a programming error or that they're trying to make it run in this way.) This example also shows us why we need to build an application to replace the program slice. (This is because applications can support menus, and patches cannot use menus directly.) We inherit from the frame at the beginning of the program. In addition, we build a builder for the class to replace the init () installation event. Finally, we create a main () method and in the new object we build, resize it and call Show (). It differs from a program only in a very small place, but it is already a standalone Setup window application and we can use the menu.