Demand:
Create a multilevel menu
Steps:
A: Create Form Objects (and set properties and layouts)
B: Create a menu bar
C: Create menus and submenus
D: Add menu Incrementally (submenu added to menu, menu added to menu bar)
E: Set menu bar in form (menu bar is not added, but set)
F: Set Listener events for each menu
G: Set the form to turn off listening time
H: Setting the display window
Code:
1 Public classFRAMETEXT4 {2 3 Public Static voidMain (string[] args) {4 5 //Create a Form object6 FinalFrame f =NewFrame ("Multilevel Menu");7 //Set Form Properties8F.setbounds (300, 300, 500, 500);9F.setlayout (NewFlowLayout ());Ten One //get the original title A FinalString str =f.gettitle (); - - //Create a menu bar theMenuBar m =NewMenuBar (); - - //Create a menu -Menu M1 =NewMenu ("Action"); +Menu m2 =NewMenu ("Change title"); - + //Create a sub-menu A FinalMenuItem MI1 =NewMenuItem ("N-level menu"); at FinalMenuItem mi2 =NewMenuItem ("M-level menu"); -MenuItem Mi3 =NewMenuItem ("Restore title"); -MenuItem Mi4 =NewMenuItem ("Exit"); -MenuItem MI5 =NewMenuItem ("Open Notepad"); - - //incrementally Add in //submenu added to menu - M2.add (MI1); to M2.add (mi2); + M2.add (MI3); - M1.add (m2); the M1.add (MI5); * M1.add (MI4); $ Panax Notoginseng //menu is added to the menu bar - M.add (M1); the + //Setting the menu bar in a form A F.setmenubar (m); the + //set Listener events for each menu -Mi1.addactionlistener (NewActionListener () { $ $ Public voidactionperformed (ActionEvent e) { - //change the name of the title bar - //to make changes later, first get the name of the Mi1 menu, and then change the F.settitle (Mi1.getlabel ()); - //Change the title name of the window to the MI1 labelWuyi } the }); - WuMi2.addactionlistener (NewActionListener () { - About Public voidactionperformed (ActionEvent e) { $ //such as Mil1 - F.settitle (Mi2.getlabel ()); - } - A }); + theMi3.addactionlistener (NewActionListener () { - //this is different from the above 2, because it is the original title restored. $ //so you have to create a string in front to get the beginning of the title, and then directly to change it can be the Public voidactionperformed (ActionEvent e) { the f.settitle (str); the } the }); - in //Exit System theMi4.addactionlistener (NewActionListener () { the About Public voidactionperformed (ActionEvent e) { the //Exit System theSystem.exit (0); the } + - }); the Bayi //Open Notepad theMi5.addactionlistener (NewActionListener () { the - Public voidactionperformed (ActionEvent e) { - //Runtime enables applications to connect to the environment in which they run the //publicStatic Runtime GetRuntime () returns the runtime object associated with the current Java application. the The //publicprocess exec (String command) executes the specified string commands in a separate process. the theRuntime r =runtime.getruntime (); - Try { theR.exec ("notepad"); the}Catch(IOException E1) { the //TODO auto-generated Catch block94 e1.printstacktrace (); the } the the }98 About }); - 101 //Setting the window to turn off listener events102F.addwindowlistener (NewWindowadapter () {103 Public voidwindowclosing (windowevent e) {104System.exit (0); the }106 });107 108 //make the form display109F.setvisible (true); the }111 the}
Creating a multi-level menu form for the Java 24-7 GUI