Java know how many (89) lists and combo boxes

Source: Internet
Author: User

There are two types of menus: a drop-down menu and a pop-up menu. This chapter only discusses the drop-down menu programming method. Menus are different from JComboBox and Jcheckbox, and they are always visible in the interface. The menu is identical to JComboBox in that only one item can be selected at a time.

Selecting an option in the drop-down menu or pop-up menu results in a ActionEvent event. The event is sent to the monitor for that option, and the meaning of the event is interpreted by the monitor.

menu items, menus, and dishes

The drop-down menu is represented by the name visible on the menu bar, and the menu bar (jmenubar) usually appears at the top of the JFrame, and a menu bar displays the names of multiple drop-down menus. You can activate the drop-down menu in two ways. One is to press the mouse button, and remain pressed, move the mouse, until the release mouse to complete the selection, the high-Brightness Display menu item is selected. Another way is when the cursor is over the menu name in the menu bar, click the mouse, in which case the menu expands, and the menu item is displayed with high brightness.

A menu bar can put multiple menus (jmenu), and each menu can have many menu items (jmenuitem). For example, the Eclipse Environment's menu bar has a menu of file, Edit, Source, Refactor, and many menu items for each menu. For example, the File menu has new, Open File, close, close all, and other menu items.

Add a menu to the window by creating a menu bar object, then creating several menu objects, placing the menu objects in the menu bar, and then adding menu items to each menu object as required.
Menu items can also be a full menu. Because a menu item can also be another full menu, you can construct a hierarchical menu structure.

1. Menu bar
The instance of Class JMenuBar is the menu bar. For example, the following code creates a menu bar object menubar:
JMenuBar menubar = new JMenuBar ();
To add a menu bar to the window, you must use the Setjmenubar () method in the JFrame class. For example, code:
Setjmenubar (menubar);

Common methods for class JMenuBar are:

    1. Add (jmenu m): Adds the menu m to the menu bar.
    2. Countjmenus (): Gets the number of menu bars in the menu bar.
    3. Getjmenu (int p): Gets the menu in the menu bar.
    4. Remove (jmenu m): Removes the menu m from the menu bar.


2. Menu
The object created by the class JMenu is the menu. The common methods of class JMenu are as follows:

    1. JMenu (): Creates a menu with an empty caption.
    2. JMenu (String s): Create a menu titled S.
    3. Add (JMenuItem Item): Adds the menu option specified by the parameter item to the menu.
    4. Add (JMenu menu): Adds the menu specified by the Parameters menu to the menus. Implements the Embed submenu in the menu.
    5. AddSeparator (): Draw a divider between menu options.
    6. GetItem (int n): Gets the menu item at the specified index.
    7. GetItemCount (): Gets the number of menu items.
    8. Insert (JMenuItem item,int N): Inserts the menu item in the menu position n item.
    9. Remove (int n): Remove menu item with the position n
    10. RemoveAll (): Removes all menu items from the menu.


3. Menu items
The instance of Class JMenuItem is the menu item. The common methods of class JMenuItem are as follows:

    1. JMenuItem (): Constructs a untitled menu item.
    2. JMenuItem (String s): Constructs a menu item with a caption.
    3. SetEnabled (Boolean B): Sets whether the current item can be selected.
    4. IsEnabled (): Returns whether the current menu item can be selected by the user.
    5. Getlabel (): Gets the name of the menu item.
    6. SetLabel (): Sets the name of the menu option.
    7. addActionListener (ActionListener E): Sets the monitor for the menu item. The monitor accepts an action event that clicks on a menu.


4. Handling Menu Events
The event source for a menu is to click a menu item with the mouse. The interface that handles the event is ActionListener, and the interface method to be implemented is actionperformed (ActionEvent e), which gets the method GetSource () of the event source.

"Example 11-15" small application schematic window has a menu bar implementation method. There is a button, when the button is in the Open window state, click the button will open a window, the window has a menu bar, there are two menus, each menu has three items. When a menu item is selected, the menu item monitoring method displays the selected typeface in the text box.

1 Importjava.applet.*2 Importjavax.swing.*;3 Importjava.awt.*;4 Importjava.awt.event.*;5 classMenuwindowextendsJFrameImplementsactionlistener{6      Public StaticJTextField text;7     Private voidAddItem (jmenu menu,string Menuname,actionlistener listener) {8JMenuItem Anitem =NewJMenuItem (menuName);9 Anitem.setactioncommand (menuName);Ten Anitem.addactionlistener (listener); One menu.add (anitem); A     } -      PublicMenuwindow (String S,intWinth) { - Settitle (s); theContainer con = This. Getcontentpane (); -Con.setlayout (NewBorderLayout ()); -          This. setlocation (100,100); -          This. SetSize (w,h); +JMenu menu1 =NewJMenu ("Sport"); -AddItem (menu1, "Running", This); +AddItem (menu1, "rope Skipping", This); AAddItem (Menu1, "Play Ball", This); atJMenu menu2 = JMenu ("Entertainment"); -AddItem (MENU2, "singing", This); -AddItem (MENU2, "Dancing", This); -AddItem (MENU2, "game", This); -JMenuBar menubar =NewJMenuBar (); -Text =NewJTextField (); in Menubar.add (menu1); - Menubar.add (MENU2); to Setjmenubar (MenuBar); + Con.add (Text,borderlayout.north); -     } the      Public voidactionperformed (ActionEvent e) { *Text.settext (E.getactioncommand () + "menu item is selected!"); $     }Panax Notoginseng } -  Public classExample6_5extendsAppletImplementsactionlistener{ the Menuwindow window; + JButton button; A     BooleanBFLG; the      Public voidinit () { +button =NewJButton ("Open my window of sports Entertainment"); BFLG =true; -window =NewMenuwindow ("Window of sport and entertainment", 100,100); $Button.addactionlistener ( This); $ Add (button); -     } -      Public voidactionperformed (ActionEvent e) { the         if(E.getsource () = =button) { -             if(BFLG) {WuyiWindow.setvisible (true); theBFLG =false; -Button.setlabel ("Close the window of my sports entertainment"); Wu             } -             Else{ AboutWindow.setvisible (false); $BFLG =true; -Button.setlabel ("Open the window of my sports entertainment"); -             } -         } A     } +}


5. Embed sub-menu
Creates a menu and creates multiple menu items, where a menu item is another menu (with other items), which makes the menu nested. For example, change the code in the above program to the following:
Menu menu1,menu2,item4;
MenuItem item3,item5,item6,item41,item42;
Also insert the following code to create the Item41 and ITEM42 menu items and add them to the Item4 menu:
item41= New MenuItem ("Eastern Red");
ITEM42 = new MenuItem ("Peony");
Item4.add (ITEM41);
Item4.add (ITEM42);
When you click on the Item4 menu, you will also be offered a selection of two menu items.

6. Add an exit to the menu
Add a new menu item, monitor the menu item, and use the System.exit () method in the corresponding monitoring method to exit the Java Runtime Environment when the menu item is clicked. For example, the following schematic code:

1 ... ..2Item7 =NewMenuItem ("exit");3Item7.addactionlistener ( This);4 ... ..5  Public voidactionperformed (ActionEvent e) {6     if(E.getsource () = =Item7) {7System.exit (0);8     }9}

7. Set shortcut keys for menu items

Use the Menushortcut class to set shortcut keys for menu items. The construction method is menushortcut (int key). Where key can be value keyevent.vk_a to Kenevent.vk_z, you can also take ' a ' to ' Z ' key code value. Menu items Use the Setshortcut (menushortcut K) method to set shortcut keys. For example, the following code sets the letter E as a shortcut key.

1 classHerwindowextendsFrameImplementsactionlistener{2 MenuBar Menbar;3 menu menu;4 MenuItem Item;5Menushortcut shortcut =Newmenushortcut (keyevent.vk_e);6 ... ..7 item.setshortcut (shortcut);8 ... ..9}

Select Box menu item

Menus can also contain options that have a persistent selection state, a special menu that can be defined by the JCheckBoxMenuItem class. The JCheckBoxMenuItem object, like a selection box, can indicate whether an option is selected or not, or it can be added to a drop-down menu as a menu item. When you click on the JCheckBoxMenuItem menu, tick marks or clear tick marks will appear on the left side of it. For example, in the class Menuwindow of the example 6.5 program, the code
AddItem (menu1, "Run", this); AddItem (menu1, "rope Skipping", this);
Rewrite the following code to change the two general menu items "Running" and "skipping" to two selection box menu items:

1JCheckBoxMenuItem item1 =NewJCheckBoxMenuItem ("Running");2JCheckBoxMenuItem item2 =NewJCheckBoxMenuItem ("Rope Skipping");3 Item1.setactioncommand ("Running");4Item1.addactionlistener ( This);5 Menu1.add (item1);6 Item2.setactioncommand ("Rope Skipping");7Item2.addactionlistener ( This);8Menu1.add (ITEM2);

Series Articles:

Java know how much (top)Java know how much (medium)Java knows how many () Java vectors (vector) and their applicationsJava know how much (79) hash table and its applicationJava know how much (80) graphical Interface design basicsJava know how much (81) frame window BasicsJava know how much (82) Introduction to tags, buttons, and button eventsJava know how much (83) Panel Basics: JPanel and JScrollPaneJava know how much (84) layout design of graphical interfaceJava know how much (85) text box and text areaJava know how much (86) input and output of text box and text areaJava know how much (87) Select boxes and radio buttonsJava know how many (88) lists and combo boxes

Java know how many (89) lists and combo boxes

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.