Documentation

The Java™ Tutorials
Hide TOC
How to Use Menus如何使用菜单
Trail: Creating a GUI With Swing
Lesson: Using Swing Components
Section: How to Use Various Components

How to Use Menus如何使用菜单

A menu provides a space-saving way to let the user choose one of several options. 菜单提供了一种节省空间的方式,让用户选择几个选项之一。Other components with which the user can make a one-of-many choice include combo boxes, lists, radio buttons, spinners, and tool bars. 用户可以选择的其他组件包括组合框列表单选按钮微调器工具栏If any of your menu items performs an action that is duplicated by another menu item or by a tool-bar button, then in addition to this section you should read How to Use Actions.如果任何菜单项执行的操作被另一个菜单项或工具栏按钮复制,则除本节外,您还应阅读如何使用操作

Menus are unique in that, by convention, they aren't placed with the other components in the UI. 菜单的独特之处在于,按照惯例,它们不会与UI中的其他组件一起放置。Instead, a menu usually appears either in a menu bar or as a popup menu. 相反,菜单通常显示在菜单栏弹出菜单中。A menu bar contains one or more menus and has a customary, platform-dependent location — usually along the top of a window. 菜单栏包含一个或多个菜单,并具有常规的、依赖于平台的位置;通常沿着窗户的顶部。A popup menu is a menu that is invisible until the user makes a platform-specific mouse action, such as pressing the right mouse button, over a popup-enabled component. 弹出菜单是一个不可见的菜单,直到用户在启用弹出菜单的组件上进行特定于平台的鼠标操作,如按下鼠标右键。The popup menu then appears under the cursor.然后,弹出菜单出现在光标下方。

The following figure shows many menu-related components: a menu bar, menus, menu items, radio button menu items, check box menu items, and separators. 下图显示了许多与菜单相关的组件:菜单栏、菜单、菜单项、单选按钮菜单项、复选框菜单项和分隔符。As you can see, a menu item can have either an image or text, or both. You can also specify other properties, such as font and color.如您所见,菜单项可以包含图像或文本,也可以同时包含图像和文本。您还可以指定其他属性,如字体和颜色。

MenuLookDemo

The rest of this section teaches you about the menu components and tells you how to use various menu features:本节的其余部分将向您介绍菜单组件,并告诉您如何使用各种菜单功能:

The Menu Component Hierarchy菜单组件层次结构

Here is a picture of the inheritance hierarchy for the menu-related classes:以下是菜单相关类的继承层次结构图:

The inheritance hierarchy for menu classes

As the figure shows, menu items (including menus) are simply buttons. 如图所示,菜单项(包括菜单)只是按钮You might be wondering how a menu, if it's only a button, shows its menu items. 您可能想知道,如果菜单只是一个按钮,它是如何显示菜单项的。The answer is that when a menu is activated, it automatically brings up a popup menu that displays the menu items.答案是,当菜单被激活时,它会自动弹出菜单,显示菜单项。

Creating Menus创建菜单

The following code creates the menus shown near the beginning of this menu section. 下面的代码创建了显示在此菜单部分开头附近的菜单。The bold lines of code create and connect the menu objects; the other code sets up or customizes the menu objects. 粗体代码行创建并连接菜单对象;其他代码设置或自定义菜单对象。You can find the entire program in MenuLookDemo.java. 您可以在MenuLookDemo.java中找到整个程序。Other required files are listed in the example index. 示例索引中列出了其他所需文件。


Try this: 

Because this code has no event handling, the menus do nothing useful except to look as they should. 因为这段代码没有事件处理,所以菜单除了看起来应该的样子之外没有任何用处。If you run the example, you'll notice that despite the lack of custom event handling, menus and submenus appear when they should, and the check boxes and radio buttons respond appropriately when the user chooses them.如果运行该示例,您会注意到,尽管缺少自定义事件处理,但菜单和子菜单在应该出现的时候出现,并且当用户选择它们时,复选框和单选按钮会相应响应。

//Where the GUI is created:
JMenuBar menuBar;
JMenu menu, submenu;
JMenuItem menuItem;
JRadioButtonMenuItem rbMenuItem;
JCheckBoxMenuItem cbMenuItem;

//Create the menu bar.
menuBar = new JMenuBar();

//Build the first menu.
menu = new JMenu("A Menu");
menu.setMnemonic(KeyEvent.VK_A);
menu.getAccessibleContext().setAccessibleDescription(
        "The only menu in this program that has menu items");
menuBar.add(menu);

//a group of JMenuItems
menuItem = new JMenuItem("A text-only menu item",
                         KeyEvent.VK_T);
menuItem.setAccelerator(KeyStroke.getKeyStroke(
        KeyEvent.VK_1, ActionEvent.ALT_MASK));
menuItem.getAccessibleContext().setAccessibleDescription(
        "This doesn't really do anything");
menu.add(menuItem);
menuItem = new JMenuItem("Both text and icon",
                         new ImageIcon("images/middle.gif"));
menuItem.setMnemonic(KeyEvent.VK_B);
menu.add(menuItem);
menuItem = new JMenuItem(new ImageIcon("images/middle.gif"));
menuItem.setMnemonic(KeyEvent.VK_D);
menu.add(menuItem);

//a group of radio button menu items
menu.addSeparator();
ButtonGroup group = new ButtonGroup();
rbMenuItem = new JRadioButtonMenuItem("A radio button menu item");
rbMenuItem.setSelected(true);
rbMenuItem.setMnemonic(KeyEvent.VK_R);
group.add(rbMenuItem);
menu.add(rbMenuItem);
rbMenuItem = new JRadioButtonMenuItem("Another one");
rbMenuItem.setMnemonic(KeyEvent.VK_O);
group.add(rbMenuItem);
menu.add(rbMenuItem);

//a group of check box menu items
menu.addSeparator();
cbMenuItem = new JCheckBoxMenuItem("A check box menu item");
cbMenuItem.setMnemonic(KeyEvent.VK_C);
menu.add(cbMenuItem);
cbMenuItem = new JCheckBoxMenuItem("Another one");
cbMenuItem.setMnemonic(KeyEvent.VK_H);
menu.add(cbMenuItem);

//a submenu
menu.addSeparator();
submenu = new JMenu("A submenu");
submenu.setMnemonic(KeyEvent.VK_S);
menuItem = new JMenuItem("An item in the submenu");
menuItem.setAccelerator(KeyStroke.getKeyStroke(
        KeyEvent.VK_2, ActionEvent.ALT_MASK));
submenu.add(menuItem);
menuItem = new JMenuItem("Another item");
submenu.add(menuItem);
menu.add(submenu);

//Build second menu in the menu bar.
menu = new JMenu("Another Menu");
menu.setMnemonic(KeyEvent.VK_N);
menu.getAccessibleContext().setAccessibleDescription(
        "This menu does nothing");
menuBar.add(menu);

...
frame.setJMenuBar(theJMenuBar);

As the code shows, to set the menu bar for a JFrame, you use the setJMenuBar method. 如代码所示,要设置JFrame的菜单栏,可以使用setJMenuBar方法。To add a JMenu to a JMenuBar, you use the add(JMenu) method. 要将JMenu添加到JMenuBar,可以使用add(JMenu)方法。To add menu items and submenus to a JMenu, you use the add(JMenuItem) method.要将菜单项和子菜单添加到JMenu,可以使用add(JMenuItem)方法。


Note: 

Menu items, like other components, can be in at most one container. 与其他组件一样,菜单项最多只能在一个容器中。If you try to add a menu item to a second menu, the menu item will be removed from the first menu before being added to the second. 如果您尝试将菜单项添加到第二个菜单,则该菜单项将在添加到第三个菜单之前从第一个菜单中删除。For a way of implementing multiple components that do the same thing, see How to Use Actions.有关实现多个组件执行相同操作的方法,请参阅如何使用操作


Other methods in the preceding code include setAccelerator and setMnemonic, which are discussed a little later in Enabling Keyboard Operation. 前面代码中的其他方法包括setAcceleratorsetMnemonic,稍后将在启用键盘操作中讨论。The setAccessibleDescription method is discussed in How to Support Assistive Technologies.如何支持辅助技术中讨论了setAccessibleDescription方法。

Handling Events from Menu Items处理菜单项中的事件

To detect when the user chooses a JMenuItem, you can listen for action events (just as you would for a JButton). 为了检测用户何时选择JMenuItem,您可以监听动作事件(就像监听JButton一样)。To detect when the user chooses a JRadioButtonMenuItem, you can listen for either action events or item events, as described in How to Use Radio Buttons. 要检测用户何时选择JRadioButtonMenuItem,您可以监听操作事件或项事件,如如何使用单选按钮中所述。For JCheckBoxMenuItems, you generally listen for item events, as described in How to Use Check Boxes.对于JCheckBoxMenuItems,您通常会侦听项事件,如如何使用复选框中所述。


Try this: 
The text area shows the action and item events our listeners detected.

Here is the code that implements the event handling:以下是实现事件处理的代码:

public class MenuDemo ... implements ActionListener,
                                     ItemListener {
    ...
    public MenuDemo() {
        //...for each JMenuItem instance:
        menuItem.addActionListener(this);
        ...
        //for each JRadioButtonMenuItem:
        rbMenuItem.addActionListener(this);
        ...
        //for each JCheckBoxMenuItem:
        cbMenuItem.addItemListener(this);
        ...
    }

    public void actionPerformed(ActionEvent e) {
        //...Get information from the action event...
        //...Display it in the text area...
    }

    public void itemStateChanged(ItemEvent e) {
        //...Get information from the item event...
        //...Display it in the text area...
    }

For examples of handling action and item events, see the button, radio button, and check box sections, as well as the list of examples at the end of this section.有关处理操作和项目事件的示例,请参阅按钮单选按钮复选框部分,以及本部分末尾的示例列表

Enabling Keyboard Operation启用键盘操作

Menus support two kinds of keyboard alternatives: mnemonics and accelerators. 菜单支持两种键盘选择:助记符和加速器。Mnemonics助记符 offer a way to use the keyboard to navigate the menu hierarchy, increasing the accessibility of programs. 提供一种使用键盘导航菜单层次结构的方法,增加程序的可访问性。Accelerators, on the other hand, offer keyboard shortcuts to bypass navigating the menu hierarchy. 另一方面,加速器提供键盘快捷键以绕过菜单层次结构的导航。Mnemonics are for all users; accelerators are for power users.助记符适用于所有用户;加速器是为超级用户设计的。

A mnemonic is a key that makes an already visible menu item be chosen. 助记符是一个键,用于选择已经可见的菜单项。For example, in MenuDemo the first menu has the mnemonic A, and its second menu item has the mnemonic B. 例如,在MenuDemo中,第一个菜单具有助记符号A,第二个菜单项具有助记字母B。This means that, when you run MenuDemo with the Java look and feel, pressing the Alt and A keys makes the first menu appear. 这意味着,当您使用Java外观运行MenuDemo时,按下Alt和A键将显示第一个菜单。While the first menu is visible, pressing the B key (with or without Alt) makes the second menu item be chosen. 当第一个菜单可见时,按B键(带或不带Alt)可选择第二个菜单项。A menu item generally displays its mnemonic by underlining the first occurrence of the mnemonic character in the menu item's text, as the following snapshot shows.菜单项通常通过在菜单项文本中第一次出现的助记符字符下下划线来显示其助记符,如下快照所示。

B is the mnemonic character for this menu item

An accelerator is a key combination that causes a menu item to be chosen, whether or not it's visible. 加速键是一种组合键,可使菜单项被选择,无论其是否可见。For example, pressing the Alt and 2 keys in MenuDemo makes the first item in the first menu's submenu be chosen, without bringing up any menus. 例如,在MenuDemo中按Alt键和2键可选择第一个菜单子菜单中的第一个项目,而不显示任何菜单。Only leaf menu items — menus that don't bring up other menus — can have accelerators. 仅叶菜单项—不显示其他菜单的菜单;它可以有加速器。The following snapshot shows how the Java look and feel displays a menu item that has an accelerator.下面的快照显示了Java外观如何显示具有加速器的菜单项。

Alt+2 advertises this menu item's accelerator

You can specify a mnemonic either when constructing the menu item or with the setMnemonic method. 您可以在构造菜单项时或使用setMnemonic方法指定助记符。To specify an accelerator, use the setAccelerator method. 要指定加速器,请使用setAccelerator方法。Here are examples of setting mnemonics and accelerators:以下是设置助记符和加速器的示例:

//Setting the mnemonic when constructing a menu item:
menuItem = new JMenuItem("A text-only menu item",
                         KeyEvent.VK_T);

//Setting the mnemonic after creation time:
menuItem.setMnemonic(KeyEvent.VK_T);

//Setting the accelerator:
menuItem.setAccelerator(KeyStroke.getKeyStroke(
        KeyEvent.VK_T, ActionEvent.ALT_MASK));

As you can see, you set a mnemonic by specifying the KeyEvent constant corresponding to the key the user should press. 如您所见,您可以通过指定与用户应按下的键对应的KeyEvent常量来设置助记符。To specify an accelerator you must use a KeyStroke object, which combines a key (specified by a KeyEvent constant) and a modifier-key mask (specified by an ActionEvent constant).要指定加速器,必须使用KeyStroke对象,该对象组合键(由KeyEvent常量指定)和修改器键掩码(由ActionEvent常量规定)。


Note: 

Because popup menus, unlike regular menus, aren't always contained by a component, accelerators in popup menu items don't work unless the popup menu is visible.因为与常规菜单不同,弹出菜单并不总是包含在组件中,所以除非弹出菜单可见,否则弹出菜单项中的加速器不起作用。


Bringing Up a Popup Menu弹出菜单

To bring up a popup menu ( JPopupMenu), you must register a mouse listener on each component that the popup menu should be associated with. 要打开弹出菜单(JPopupMenu),必须在弹出菜单应关联的每个组件上注册鼠标侦听器。The mouse listener must detect user requests that the popup menu be brought up.鼠标监听器必须检测用户请求弹出菜单。

The exact gesture that should bring up a popup menu varies by look and feel. 打开弹出菜单的确切姿势因外观和感觉而异。In Microsoft Windows, the user by convention brings up a popup menu by releasing the right mouse button while the cursor is over a component that is popup-enabled. 在Microsoft Windows中,根据惯例,当光标位于启用弹出功能的组件上时,用户通过释放鼠标右键打开弹出菜单。In the Java look and feel, the customary trigger is either pressing the right mouse button (for a popup that goes away when the button is released) or clicking it (for a popup that stays up). 在Java外观中,通常的触发是按下鼠标右键(对于释放按钮时消失的弹出窗口)或单击它(对于保持不变的弹出窗口)。


Try this: 
//...where instance variables are declared:
JPopupMenu popup;

    //...where the GUI is constructed:
    //Create the popup menu.
    popup = new JPopupMenu();
    menuItem = new JMenuItem("A popup menu item");
    menuItem.addActionListener(this);
    popup.add(menuItem);
    menuItem = new JMenuItem("Another popup menu item");
    menuItem.addActionListener(this);
    popup.add(menuItem);

    //Add listener to components that can bring up popup menus.
    MouseListener popupListener = new PopupListener();
    output.addMouseListener(popupListener);
    menuBar.addMouseListener(popupListener);
...
class PopupListener extends MouseAdapter {
    public void mousePressed(MouseEvent e) {
        maybeShowPopup(e);
    }

    public void mouseReleased(MouseEvent e) {
        maybeShowPopup(e);
    }

    private void maybeShowPopup(MouseEvent e) {
        if (e.isPopupTrigger()) {
            popup.show(e.getComponent(),
                       e.getX(), e.getY());
        }
    }
}

Popup menus have a few interesting implementation details. 弹出菜单有一些有趣的实现细节。One is that every menu has an associated popup menu. When the menu is activated, it uses its associated popup menu to show its menu items.一个是每个菜单都有一个关联的弹出菜单。当菜单被激活时,它使用其关联的弹出菜单显示其菜单项。

Another detail is that a popup menu itself uses another component to implement the window containing the menu items. 另一个细节是弹出菜单本身使用另一个组件来实现包含菜单项的窗口。Depending on the circumstances under which the popup menu is displayed, the popup menu might implement its "window" using a lightweight component (such as a JPanel), a "mediumweight" component (such as a Panel), or a heavyweight window (something that inherits from Window).根据弹出菜单的显示环境,弹出菜单可能使用轻量级组件(如JPanel)、中等权重组件(如Panel)或重量级窗口(从Window继承的东西)实现其“窗口”。

Lightweight popup windows are more efficient than heavyweight windows but, prior to the Java SE Platform 6 Update 12 release, they didn't work well if you had any heavyweight components inside your GUI. 轻量级弹出窗口比重量级窗口更高效,但在JavaSE平台6更新12发布之前,如果GUI中有任何重量级组件,它们就不能很好地工作。Specifically, when the lightweight popup's display area intersects the heavyweight component's display area, the heavyweight component is drawn on top. 具体而言,当轻量级弹出窗口的显示区域与重量级组件的显示区域相交时,重量级组件将绘制在顶部。This is one of the reasons that, prior to the 6u12 release, we recommended against mixing heavyweight and lightweight components. 这是在6u12发布之前,我们建议不要混合重量级和轻型组件的原因之一。If you are using an older release and absolutely need to use a heavyweight component in your GUI, then you can invoke JPopupMenu.setLightWeightPopupEnabled(false) to disable lightweight popup windows. 如果您使用的是较旧版本,并且绝对需要在GUI中使用重量级组件,则可以调用JPopupMenu.setLightWeightPopupEnabled(false)来禁用轻量级弹出窗口。For information on mixing components in the 6u12 release and later, please see Mixing Heavyweight and Lightweight Components.有关6u12版本及更高版本中混合组件的信息,请参阅混合重量级和轻型组件

Customizing Menu Layout自定义菜单布局

Because menus are made up of ordinary Swing components, you can easily customize them. 因为菜单是由普通的Swing组件组成的,所以您可以轻松地自定义它们。For example, you can add any lightweight component to a JMenu or JMenuBar. 例如,您可以将任何轻量级组件添加到JMenuJMenuBarAnd because JMenuBar uses BoxLayout, you can customize a menu bar's layout just by adding invisible components to it. 由于JMenuBar使用BoxLayout,您可以通过向菜单栏添加不可见的组件来定制菜单栏的布局。Here is an example of adding a glue component to a menu bar, so that the last menu is at the right edge of the menu bar:以下是向菜单栏添加粘合组件的示例,以便最后一个菜单位于菜单栏的右边缘:

//...create and add some menus...
menuBar.add(Box.createHorizontalGlue());
//...create the rightmost menu...
menuBar.add(rightMenu);

Try this: 

Here's the modified menu layout that MenuGlueDemo displays:以下是MenuGlueDemo显示的修改菜单布局:

MenuGlueDemo

Another way of changing the look of menus is to change the layout managers used to control them. 更改菜单外观的另一种方法是更改用于控制菜单的布局管理器。For example, you can change a menu bar's layout manager from the default left-to-right BoxLayout to something such as GridLayout. 例如,可以将菜单栏的布局管理器从默认的从左到右的BoxLayout更改为GridLayout


Try this: 

Here's a picture of the menu layout that MenuLayoutDemo creates:以下是MenuLayoutDemo创建的菜单布局的图片:

MenuLayoutDemo

The Menu API菜单API

The following tables list the commonly used menu constructors and methods. 下表列出了常用的菜单构造函数和方法。The API for using menus falls into these categories:使用菜单的API分为以下几类:

Creating and Setting Up Menu Bars创建和设置菜单栏
Constructor or Method构造函数或方法 Purpose目的
JMenuBar() Creates a menu bar.创建菜单栏。
JMenu add(JMenu) Adds the menu to the end of the menu bar.将菜单添加到菜单栏的末尾。
void setJMenuBar(JMenuBar)
JMenuBar getJMenuBar()
(in JApplet, JDialog, JFrame, JInternalFrame, JRootPane)
Sets or gets the menu bar of an applet, dialog, frame, internal frame, or root pane.设置或获取小程序对话框框架内部框架根窗格的菜单栏。
Creating and Populating Menus创建和填充菜单
Constructor or Method构造函数或方法 Purpose目的
JMenu()
JMenu(String)
JMenu(Action)
Creates a menu. 创建菜单。The string specifies the text to display for the menu. 字符串指定要为菜单显示的文本。The Action specifies the text and other properties of the menu (see How to Use Actions).Action指定菜单的文本和其他属性(请参见如何使用操作)。
JMenuItem add(JMenuItem)
JMenuItem add(String)
Adds a menu item to the current end of the menu. 将菜单项添加到菜单的当前末尾。If the argument is a string, then the menu automatically creates a JMenuItem object that displays the specified text.如果参数是字符串,则菜单会自动创建一个显示指定文本的JMenuItem对象。
void addSeparator() Adds a separator to the current end of the menu.在菜单的当前结尾添加分隔符。
JMenuItem insert(JMenuItem, int)
void insert(String, int)
void insertSeparator(int)
Inserts a menu item or separator into the menu at the specified position. 在指定位置将菜单项或分隔符插入菜单。The first menu item is at position 0, the second at position 1, and so on. 第一个菜单项位于位置0,第二个菜单项处于位置1,以此类推。The JMenuItem and String arguments are treated the same as in the corresponding add methods.JMenuItemString参数的处理方式与相应的add方法相同。
void remove(JMenuItem)
void remove(int)
void removeAll()
Removes the specified item(s) from the menu. If the argument is an integer, then it specifies the position of the menu item to be removed.从菜单中删除指定项。如果参数是整数,则指定要删除的菜单项的位置。
Creating, Populating, and Controlling Popup Menus创建、填充和控制弹出菜单
Constructor or Method构造函数或方法 Purpose目的
JPopupMenu()
JPopupMenu(String)
Creates a popup menu. 创建一个弹出菜单。The optional string argument specifies the title that a look and feel might display as part of the popup window.可选字符串参数指定外观可能作为弹出窗口的一部分显示的标题。
JMenuItem add(JMenuItem)
JMenuItem add(String)
Adds a menu item to the current end of the popup menu. 将菜单项添加到弹出菜单的当前端。If the argument is a string, then the menu automatically creates a JMenuItem object that displays the specified text.如果参数是字符串,则菜单会自动创建一个显示指定文本的JMenuItem对象。
void addSeparator() Adds a separator to the current end of the popup menu.在弹出菜单的当前端添加分隔符。
void insert(Component, int) Inserts a menu item into the menu at the specified position. 在指定位置将菜单项插入菜单。The first menu item is at position 0, the second at position 1, and so on. 第一个菜单项位于位置0,第二个菜单项处于位置1,以此类推。The Component argument specifies the menu item to add.Component参数指定要添加的菜单项。
void remove(int)
void removeAll()
Removes the specified item(s) from the menu. 从菜单中删除指定项。If the argument is an integer, then it specifies the position of the menu item to be removed.如果参数是整数,则指定要删除的菜单项的位置。
static void setLightWeightPopupEnabled(boolean) By default, Swing implements a menu's window using a lightweight component. 默认情况下,Swing使用轻量级组件实现菜单窗口。This can cause problems if you use any heavyweight components in your Swing program, as described in Bringing Up a Popup Menu. 如果在Swing程序中使用任何重量级组件,这可能会导致问题,如弹出菜单中所述。(This is one of several reasons to avoid using heavyweight components.) (这是避免使用重型部件的几个原因之一。)As a workaround, invoke JPopupMenu.setLightWeightPopupEnabled(false).作为一种解决方法,调用JPopupMenu.setLightWeightPopupEnabled(false)
void show(Component, int, int) Display the popup menu at the specified x,y position (specified in that order by the integer arguments) in the coordinate system of the specified component.在指定组件的坐标系中指定的xy位置(以整数参数的顺序指定)显示弹出菜单。
Implementing Menu Items实现菜单项
Constructor or Method构造函数或方法 Purpose目的
JMenuItem()
JMenuItem(String)
JMenuItem(Icon)
JMenuItem(String, Icon)
JMenuItem(String, int)
JMenuItem(Action)
Creates an ordinary menu item. 创建普通菜单项。The icon argument, if present, specifies the icon that the menu item should display. 图标参数(如果存在)指定菜单项应显示的图标。Similarly, the string argument specifies the text that the menu item should display. 类似地,字符串参数指定菜单项应显示的文本。The integer argument specifies the keyboard mnemonic to use. 整数参数指定要使用的键盘助记符。You can specify any of the relevant VK constants defined in the KeyEvent class. 您可以指定KeyEvent类中定义的任何相关VK常量。For example, to specify the A key, use KeyEvent.VK_A. 例如,要指定A键,请使用KeyEvent.VK_A

The constructor with the Action parameter sets the menu item's Action, causing the menu item's properties to be initialized from the Action. 带有Action参数的构造函数设置菜单项的操作,从而使菜单项的属性从Action初始化。See How to Use Actions for details.有关详细信息,请参阅如何使用操作

JCheckBoxMenuItem()
JCheckBoxMenuItem(String)
JCheckBoxMenuItem(Icon)
JCheckBoxMenuItem(String, Icon)
JCheckBoxMenuItem(String, boolean)
JCheckBoxMenuItem(String, Icon, boolean)
Creates a menu item that looks and acts like a check box. 创建一个外观和行为类似于复选框的菜单项。The string argument, if any, specifies the text that the menu item should display. 字符串参数(如果有)指定菜单项应显示的文本。If you specify true for the boolean argument, then the menu item is initially selected (checked). 如果为布尔参数指定true,则最初选择(选中)菜单项。Otherwise, the menu item is initially unselected.否则,菜单项最初未被选中。
JRadioButtonMenuItem()
JRadioButtonMenuItem(String)
JRadioButtonMenuItem(Icon)
JRadioButtonMenuItem(String, Icon)
JRadioButtonMenuItem(String, boolean)
JRadioButtonMenuItem(Icon, boolean)
JRadioButtonMenuItem(String, Icon, boolean)
Creates a menu item that looks and acts like a radio button. 创建一个外观和行为类似于单选按钮的菜单项。The string argument, if any, specifies the text that the menu item should display. 字符串参数(如果有)指定菜单项应显示的文本。If you specify true for the boolean argument, then the menu item is initially selected. 如果为布尔参数指定true,则最初选择菜单项。Otherwise, the menu item is initially unselected.否则,菜单项最初未被选中。
void setState(boolean)
boolean getState()
(in JCheckBoxMenuItem)
Set or get the selection state of a check box menu item.设置或获取复选框菜单项的选择状态。
void setEnabled(boolean) If the argument is true, enable the menu item. 如果参数为true,则启用菜单项。Otherwise, disable the menu item.否则,禁用菜单项。
void setMnemonic(int) Set the mnemonic that enables keyboard navigation to the menu or menu item. 设置启用菜单或菜单项键盘导航的助记符。Use one of the VK constants defined in the KeyEvent class.使用KeyEvent类中定义的VK常量之一。
void setAccelerator(KeyStroke) Set the accelerator that activates the menu item.设置激活菜单项的加速器。
void setActionCommand(String) Set the name of the action performed by the menu item.设置菜单项执行的操作的名称。
void addActionListener(ActionListener)
void addItemListener(ItemListener)
Add an event listener to the menu item. 将事件侦听器添加到菜单项。See Handling Events from Menu Items for details.有关详细信息,请参阅处理菜单项中的事件
void setAction(Action) Set the Action associated with the menu item. 设置与菜单项关联的ActionSee How to Use Actions for details.有关详细信息,请参阅如何使用操作
Many of the preceding methods are inherited from AbstractButton. 前面的许多方法都继承自AbstractButtonSee The Button API for information about other useful methods that AbstractButton provides.有关AbstractButton提供的其他有用方法的信息,请参阅按钮API

Examples that Use Menus使用菜单的示例

Menus are used in a few of our examples.在一些示例中使用了菜单。

Example示例 Where Described描述位置 Notes备注
MenuLookDemo This section (Creating Menus)本节(创建菜单 A simple example that creates all kinds of menus except popup menus, but doesn't handle events from the menu items.一个简单的示例,创建除弹出菜单之外的所有菜单,但不处理菜单项中的事件。
MenuDemo This section (Handling Events from Menu Items)本节(处理菜单项中的事件 Adds event handling to MenuLookDemo.将事件处理添加到MenuLookDemo
PopupMenuDemo This section (Bringing Up a Popup Menu)本节(弹出菜单 Adds popup menus to MenuDemo.将弹出菜单添加到MenuDemo
MenuGlueDemo This section (Customizing Menu Layout)本节(自定义菜单布局 Demonstrates affecting menu layout by adding an invisible components to the menu bar.演示如何通过向菜单栏添加不可见组件来影响菜单布局。
MenuLayoutDemo This section (Customizing Menu Layout)本节(自定义菜单布局) Implements sideways-opening menus arranged in a vertical menu bar.实现在垂直菜单栏中排列的横向打开菜单。
MenuSelectionManagerDemo Adds highlight detection to MenuDemo. 将高亮显示检测添加到MenuDemoTo see this feature, click a menu and then move the mouse over any menu item or submenu. 要查看此功能,请单击菜单,然后将鼠标移到任何菜单项或子菜单上。Once per second, the text area will be updated with information about the currently highlighted menu item, not to be confused with the menu item that the user eventually chooses. 每秒一次,文本区域将更新当前突出显示的菜单项的信息,以避免与用户最终选择的菜单项混淆。This demo uses the default MenuSelectionManager, which tracks the state of the menu hierarchy.此演示使用默认的MenuSelectionManager,它跟踪菜单层次结构的状态。
ActionDemo How to Use Actions如何使用动作 Uses Action objects to implement menu items that duplicate functionality provided by tool bar buttons.使用Action对象实现复制工具栏按钮提供的功能的菜单项。
Framework Brings up multiple identical frames, each with a menu in its menu bar.显示多个相同的帧,每个帧的菜单栏中都有一个菜单。
InternalFrameDemo How to Use Internal Frames如何使用内部框架 Uses a menu item to create windows.使用菜单项创建窗口。

See the Using JavaFX UI Controls: Menu tutorial to learn how to create menus in JavaFX.


Previous page: How to Use Lists
Next page: How to Use Panels