1. First: Menu components
Menubar,menu,menuitem
Create a menu bar, create a menu, and set up menu items in each menu.
The menu can also be added to the menu as a submenu.
Add a menu to the frame by using the Setmenubar () method.
2. Case code:
Packagecn.itcast_08;Importjava.awt.FlowLayout;ImportJava.awt.Frame;ImportJava.awt.Menu;ImportJava.awt.MenuBar;ImportJava.awt.MenuItem;Importjava.awt.event.ActionEvent;ImportJava.awt.event.ActionListener;ImportJava.awt.event.WindowAdapter;Importjava.awt.event.WindowEvent;/** Level Menu*/ Public classFramedemo { Public Static voidMain (string[] args) {//Create a Form object and set propertiesFrame f =NewFrame ("Level menu"); F.setbounds (400, 200, 400, 300); F.setlayout (NewFlowLayout ()); //Create a menu barMenuBar MB =NewMenuBar (); //Create a menuMenu m =NewMenu ("File"); //Create a menu itemMenuItem mi =NewMenuItem ("Exit system"); //Who adds who?M.add (MI); Mb.add (m); //set menu barF.setmenubar (MB); //set the form to closeF.addwindowlistener (NewWindowadapter () {@Override Public voidwindowclosing (windowevent e) {system.exit (0); } }); Mi.addactionlistener (NewActionListener () {@Override Public voidactionperformed (ActionEvent e) {system.exit (0); } }); //Set Form VisibleF.setvisible (true); }}
Operating effect:
GUI Programming Note (Java) 10:gui implementation of the first level menu