I looked at the menu and it was really drunk .. It's hard to remember .. Sad
Menus are classified into menu items. Menu items
1 menu bar
The jmenubar subclass of jcomponent is used to create Menu Bars.
Setjmenubar (jmenubar bar); only one menu bar can be added to the window.
2 menu
The jmenu subclass of jcomponent is used to create menus.
Jmenu (string S): Creates a menu named S.
Public void add (jmenuitem item): Add a menu item to the menu.
Public jmenuitem getitem (int n): Obtain the menu options at the specified index.
Public int getitemcount (): obtain the number of menu options.
3. Menu items
The jmenuitem sub-class of jcomponent is used to create menu items.
Jmenuitem (string S): Creates a menu item titled "S.
Jmenuitem (string test, icon): Creates menu items with titles and icons.
Public void setaccelerator (keystroke): sets the shortcut key for the menu item.
Pure hands .. Exhausted
Main class:
Public class gui_window {public static void main (string [] ARGs) {windowmenu W1 = new windowmenu ("window with menu", 20, 30, 280,200 );}}
Rewrite the jframe class.
Import javax. swing. *; import Java. AWT. event. *; import static javax. swing. jframe. *; public class windowmenu extends jframe {jmenubar menubar; jmenu menu, submenu; jmenuitem Item1, item2; Public windowmenu () {} public windowmenu (string S, int X, int y, int W, int h) {Init (s); setlocation (x, y); setsize (W, H); setvisible (true); setdefaclocloseoperation (dispose_on_close );} void Init (string s) {settitle (s); menubar = new jmenubar (); Menu = new jmenu ("menu"); submenu = new jmenu ("sub menu"); Item1 = new jmenuitem ("menu item 1 ", new imageicon ("a.gif"); item2 = new jmenuitem ("menu item 2", new imageicon ("B .gif"); item1.setaccelerator (keystroke. getkeystroke ('A'); // you can use item2.setaccelerator (keystroke. getkeystroke (keyevent. vk_y, inputevent. ctrl_mask); menu. add (Item1); menu. addseparator (); menu. add (item2); menu. add (submenu); submenu. add (New jmenuitem ("Hello World! ", New imageicon (" c.gif "); menubar. Add (menu); setjmenubar (menubar );}}
The effect is as follows:
Gui programming 2-menu