Java GUI (menu design)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.