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. Menu is different from JComboBox and JCheckBox, and they are always visible in the interface. The same thing about the menu with JComboBox is that only one item can be selected at a time.
Selecting an option in the drop-down menu or pop-up menu generates an ActionEvent event. The event is sent to the monitor of that option, and the meaning of the event is explained by the monitor.
Menu bars, menus and menu items
The drop-down menu is visualized by the name that appears on the menu bar. The menu bar (JMenuBar) usually appears at the top of the JFrame, and one menu bar displays the names of multiple drop-down menus. There are two ways to activate the drop-down menu. One is to press the mouse button and keep it pressed, move the mouse until the mouse is released to complete the selection, and the menu item displayed with high brightness is the selected. Another way is to click the mouse when the cursor is on the menu name in the menu bar, in which case the menu expands and the menu items are displayed with high brightness.
A menu bar can hold multiple menus (JMenu), and each menu can have many menu items (JMenuItem). For example, the menu bars of the Eclipse environment include File, Edit, Source, Refactor and other menus, and each menu has many menu items. For example, the File menu includes menu items such as New, Open File, Close, Close All.
The method of adding menus to the window is: first create a menu object, then create several menu objects, place these menu objects in the menu bar, and then add menu items to each menu object as required.
Menu items in the menu can also be a complete menu. Since the menu item can again be another complete menu, a hierarchical menu structure can be constructed.
1. Menu bar
An example of JMenuBar is a menu bar. For example, the following code creates a menubar object menubar:
JMenuBar menubar = new JMenuBar();
To add a menu bar in the window, you must use the setJMenuBar() method in the JFrame class. For example, code:
setJMenuBar(menubar);
Common methods of JMenuBar are:
2. Menu <br />The object created by class JMenu is the menu. Common methods of JMenu class are as follows:
3. Menu Item <br /> An example of class JMenuItem is a menu item. Common methods of class JMenuItem are as follows:
4. Handle menu events <br />The event source of the menu is to click a menu item with the mouse. The interface that handles this event is ActionListener, and the interface method to be implemented is actionPerformed(ActionEvent e), and the method to obtain the event source getSource().
[Example] The implementation method of the small application diagram window has a menu bar. There is a button. When the button is in the open window state, clicking the button will open a window. The window has a menu bar, with two menus, and each menu has three menu items. When a menu item is selected, the menu item monitoring method displays the corresponding menu item selected in the text box.
import java.applet.*import javax.swing.*;import java.awt.*;import java.awt.event.*;class MenuWindow extends JFrame implements ActionListener{ public static JtextField text; private void addItem(JMenu Menu,String menuName ,ActionListener listener){ JMenuItem anItem = new JMenuItem(menuName); anItem.setActionCommand(menuName); anItem.addActionListener(listener); Menu.add (anItem); } public MenuWindow(String s,int w,int h){ setTitle (s); Container con = this.getContentPane(); con.setLayout(new BorderLayout()); this.setLocation(100,100); this.setSize(w,h); JMenu menu1 = new JMen u("Sports"); addItem(menu1," Running",this); addItem(menu1," Jumping rope",this); addItem(menu1," Playing Ball",this); JMenu menu2 = JMenu("Entertainment"); addItem(menu2,"Singing" ,this); addItem(menu2,"Dancing",this); addItem(menu2,"Game",this); JMenuBar menubar = new JMenuBar(); text = new JTextField(); menubar.add(menu1); men ubar. add(menu2); setJMenuBar(MenuBar); con.add(text,BorderLayout.NORTH); } public void actionPerformed(ActionEvent e){ text.setText(e.getActionComman d()+"Menu item is selected!"); } }public class Example6_5 extends Applet implements ActionListener{ MenuWindow window; JButton button; boolean bflg; public void init(){ button = new JBut ton("Open my sports and entertainment window");bflg =true; window = new MenuWindow(" Sports and Entertainment Window",100,100); button.addActionListener(this); add(button); } public void actionPerformed(ActionEvent e){ if(e.getSource()==button){ if (bflg){ window.setVisible (true); bflg = false; button.setLabel("Close my sports entertainment window"); } else{ window.setVisible(false); bflg = true; button.setLabel("Open my sports entertainment window" : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : ); } } }} 5. Embed submenu <br />A menu is created and multiple menu items are created, and one of the menu items is another (including other menu items) menu, which constitutes menu nesting. For example, change the relevant code in the above program to the following:
Menu menu1,menu2,item4; MenuItem item3,item5,item6,item41,item42;
Insert the following code to create item41 and item42 menu items and add them to the item4 menu:
item41= new MenuItem("Dongfanghong"); item42 = new MenuItem("Peony"); item4.add(item41); item4.add(item42);
Then when you click on the item4 menu, two menu items will be opened for selection.
6. Add an exit item to the menu <br />Add a new menu item to add monitoring to the menu item. Use the System.exit() method in the corresponding monitoring method to exit Java running when clicking the menu item. environment. For example, the following schematic code:
…item7 = new MenuItem("Exit");item7.addActionListener(this);…public void actionPerformed(ActionEvent e){if(e.getSource()==item7){System.e xit(0);}} 7. Set shortcut keys for menu items <br />Use MenuShortcut class to set shortcut keys for menu items. The construction method is MenuShortcut(int key). The key can be used as the values KeyEvent.VK_A to KenEvent.VK_Z, or as the '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.
class Herwindow extends Frame implements ActionListener{ MenuBar menubar; Menu menu; MenuItem item; MenuShortcut shortcut = new MenuShortcut(KeyEvent.VK_E); … item.setShortcut(shortcut); …}Select Box Menu Items
Menu can also contain options with persistent selection states, and this special menu can be defined by the JCheckBoxMenuItem class. Like a selection box, the JCheckBoxMenuItem object can indicate whether an option is selected or not, or it can be added to the drop-down menu as a menu item. When you click the JCheckBoxMenuItem menu, the tick symbol appears on the left side of it or clear the tick symbol. For example, in the MenuWindow class of the program in Example 6.5, put the code
addItem(menu1,"running", this); addItem(menu1,"skipping rope", this);
Rewriting it into the following code, changing the two ordinary menu items "Running" and "Run Jumping" into two selection box menu items:
JCheckBoxMenuItem item1 = new JCheckBoxMenuItem("Running"); JCheckBoxMenuItem item2 = new JCheckBoxMenuItem("Running"); item1.setActionCommand("Running" ”); item1.addActionListener(this); menu1.add(item1); item2.setActionCommand(" skip rope”); item2.addActionListener(this); menu1.add(item2);