Import java.awt.*;
Import java.awt.event.*;
public class Mymenudemo {
Defining forms
Private Frame F;
Defining the menu bar
Private MenuBar MB;
Defining menus
Private Menu M,SM;
Defining menu Items
Private MenuItem Closeitem,subitem;
Defining constructors
Public Mymenudemo () {
Add the init () method to the constructor.
Init ();
}
Defines the method that sets the interface of the UI.
public void Init ()
{
Set up basic information for a form
F=new Frame ("my Window");
F.setbounds (100,300,500,600);
F.setlayout (New FlowLayout ());
Initialize Menu
M=new Menu ("file");
Sm=new Menu ("submenu");
Initializing menu items
Subitem=new MenuItem ("sub-entry");
Closeitem=new MenuItem ("exit");
Initializing the menu bar
Mb=new MenuBar ();
Add a menu to the menu bar
Mb.add (m);
Add a menu item or submenu to a menu
Sm.add (subitem);
M.add (SM);
M.add (Closeitem);
Add a menu bar to a form
F.setmenubar (MB);
Add Listener
MyEvent ();
Set form visible
F.setvisible (TRUE);
}
Defining the MyEvent method to add listeners
public void MyEvent ()
{
Add an activity listener for the menu item Closeitem
Closeitem.addactionlistener (new ActionListener () {
@Override
The Actionperformed abstract method adds a processing action.
public void actionperformed (ActionEvent e) {
TODO auto-generated method stubs
System.exit (0);
}
});
The form calls the Addwindowlistener method, passing an interface Class (Listener adapter) Windowadapter.
F.addwindowlistener (New Windowadapter () {
Replication Windowclosing method.
public void windowclosing (WindowEvent e) {
System.exit (0);
}
});
}
public static void Main (string[] args) {
New Mymenudemo ();
}
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java GUI (menu design)