Import java.awt.toolkit;
Import java.awt.event.actionevent;
Import java.awt.event.actionlistener;
Import javax.swing.jframe;
Import javax.swing.jmenu;
Import javax.swing.jmenubar;
Import javax.swing.j menuitem;
Import javax.swing.jooptionpane;
/**
* 20 10- 1-29
* @ Author Zhang Ruimin
* Swing three-level menu example
*/
Common class test extension JFrame implements ActionListener {
Private static final int WIDTH = 300
Private static final int HEIGHT = 600
private JMenuItem JMenuItem 2 _ 4 = null;
Test () {
toolkit kit = toolkit . getdefaulttoolkit(); //Set the top container frame to the center.
dimension screenSize = kit . getscreensize();
int width = screenSize.width
int height = screenSize.height
int x =(WIDTH-WIDTH)/2;
int y =(HEIGHT-HEIGHT)/2;
this.setLocation(x,y);
SetTitle ("three-level menu");
SetSize (width, height);
init();
this . setdefaultcloseoperation(EXIT _ ON _ CLOSE);
set visible(true);
}
Private void init() {
JMenuBar menuBar = new JMenuBar();
JMenu JMenu 1 = new JMenu(" menu 1 ");
JMenu jMenu2 = new JMenu ("submenu 2"); //Because there is a submenu below, it is a JMenu type.
J menuitem j menuitem 2 _1= new j menuitem ("submenu1");
jmenu 1 . add(jmenu item 2 _ 1);
J menuitem j menuitem1= new j menuitem ("secondary menu1");
J menuitem j menuitem 2 = new j menuitem ("secondary menu 2");
JMenu jMenuItem3 = new JMenu ("secondary menu 3"); //There are submenus below.
J menuitem j menuitem 3 _1= new j menuitem ("three-level menu1");
J menuitem j menuitem 3 _ 2 = new j menuitem ("three-level menu 2");
J menuitem j menuitem 3 _ 3 = new j menuitem ("three-level menu 3");
jmenuitem 3 . add(jmenuitem 3 _ 1);
jmenuitem 3 . add(jmenuitem 3 _ 2);
jmenuitem 3 . add(jmenuitem 3 _ 3);
jmenu 2 . add(jmenu item 1);
jmenu 2 . add(jmenu item 2);
jmenu 2 . add(jmenu item 3);
jmenu 1 . add(jmenu 2);
J menuitem j menuitem 2 _ 2 = new j menuitem ("submenu 3");
jmenu 1 . add(jmenu item 2 _ 2);
J menuitem j menuitem 2 _ 3 = new j menuitem ("submenu 4");
jmenu 1 . add(jmenu item 2 _ 3);
jmenu 1 . add separator();
JMenuItem 2 _ 4 = new JMenuItem(" exit ");
jmenu 1 . add(jmenu item 2 _ 4);
jmenuitem 2 _ 4 . addactionlistener(this); //Register for monitoring
menubar . add(jmenu 1);
this . setjmenubar(menuBar); //Add a menu to the panel instead of this.add(menuBar).
this . set visible(true);
}
@ Overlay
public void action performed(action event e){
if(e . getsource()= = jmenuitem 2 _ 4){
Intflag = Options pane. ShowConfirmDiarog (null, "Are you sure you want to quit?" );
system . out . println(" flag = "+flag);
if (flag == 0) {
system . out . println(" exit ");
} else if (flag == 1) {
system . out . println(" No ");
}
}
}
Public static void main(String[] args) {
New test ();
}
}