Gui programming 2-menu

Source: Internet
Author: User

I looked at the menu and it was really drunk .. It's hard to remember .. Sad

Menus are classified into menu items. Menu items

1 menu bar


The jmenubar subclass of jcomponent is used to create Menu Bars.

Setjmenubar (jmenubar bar); only one menu bar can be added to the window.


2 menu


The jmenu subclass of jcomponent is used to create menus.

Jmenu (string S): Creates a menu named S.

Public void add (jmenuitem item): Add a menu item to the menu.

Public jmenuitem getitem (int n): Obtain the menu options at the specified index.

Public int getitemcount (): obtain the number of menu options.


3. Menu items


The jmenuitem sub-class of jcomponent is used to create menu items.

Jmenuitem (string S): Creates a menu item titled "S.

Jmenuitem (string test, icon): Creates menu items with titles and icons.

Public void setaccelerator (keystroke): sets the shortcut key for the menu item.

Pure hands .. Exhausted

Main class:

Public class gui_window {public static void main (string [] ARGs) {windowmenu W1 = new windowmenu ("window with menu", 20, 30, 280,200 );}}

Rewrite the jframe class.

Import javax. swing. *; import Java. AWT. event. *; import static javax. swing. jframe. *; public class windowmenu extends jframe {jmenubar menubar; jmenu menu, submenu; jmenuitem Item1, item2; Public windowmenu () {} public windowmenu (string S, int X, int y, int W, int h) {Init (s); setlocation (x, y); setsize (W, H); setvisible (true); setdefaclocloseoperation (dispose_on_close );} void Init (string s) {settitle (s); menubar = new jmenubar (); Menu = new jmenu ("menu"); submenu = new jmenu ("sub menu"); Item1 = new jmenuitem ("menu item 1 ", new imageicon ("a.gif"); item2 = new jmenuitem ("menu item 2", new imageicon ("B .gif"); item1.setaccelerator (keystroke. getkeystroke ('A'); // you can use item2.setaccelerator (keystroke. getkeystroke (keyevent. vk_y, inputevent. ctrl_mask); menu. add (Item1); menu. addseparator (); menu. add (item2); menu. add (submenu); submenu. add (New jmenuitem ("Hello World! ", New imageicon (" c.gif "); menubar. Add (menu); setjmenubar (menubar );}}

The effect is as follows:

Gui programming 2-menu

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.