Menus MenuBar Menu MenuItem
Call the Setmenubar () method of the Frame object , set menu, Parameters:MenuBar Object
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; Public classMenubardemo {Privateframe frame; PrivateMenuBar MenuBar; Privatemenu Menu; PrivateMenu submenu; PrivateMenuItem MenuItem; PublicMenubardemo () {init (); Addevents (); } Public voidinit () {frame=NewFrame ("Menu Test"); Frame.setbounds (300, 200, 500, 400); Frame.setlayout (NewFlowLayout ()); //Menu BarMenuBar =NewMenuBar (); //Menumenu =NewMenu ("File"); //menu itemMenuItem =NewMenuItem ("Exit"); //Menu Add Menu itemmenu.add (MenuItem); //menu bar Add menumenubar.add (menu); Frame.setmenubar (MenuBar); Frame.setvisible (true); } Public voidaddevents () {Frame.addwindowlistener (NewWindowadapter () {@Override Public voidwindowclosing (windowevent e) {system.exit (0); } }); //Menu clickMenuitem.addactionlistener (NewActionListener () {@Override Public voidactionperformed (ActionEvent e) {system.exit (0); } }); } /** * @paramargs*/ Public Static voidMain (string[] args) {NewMenubardemo (); }}
[Javase] GUI (menu)