模块 java.desktop

类 JMenu

所有已实现的接口:
ImageObserver , ItemSelectable , MenuContainer , Serializable , Accessible , MenuElement , SwingConstants

@JavaBean (description ="A popup window containing menu items displayed in a menu bar.") public class JMenu extends JMenuItem implements Accessible , MenuElement
菜单的实现——一个包含 JMenuItem 的弹出窗口,当用户在 JMenuBar 上选择一个项目时显示。除了JMenuItems,一个JMenu也可以包含JSeparators。

本质上,菜单是一个带有关联 JPopupMenu 的按钮。当按下“按钮”时,JPopupMenu 出现。如果“按钮”在 JMenuBar 上,则菜单是顶级窗口。如果“按钮”是另一个菜单项,那么JPopupMenu 就是“向右拉”菜单。

Action s 可以配置菜单,并在某种程度上控制菜单。使用带有菜单的 Action 比直接配置菜单有很多好处。有关详细信息,请参阅 摇摆组件支持Action,您可以在 如何使用动作 中找到更多信息,这是 The Java Tutorial 中的一个部分。

有关使用菜单的信息和示例,请参阅 如何使用菜单The Java Tutorial. 中的一个部分

Warning: Swing 不是线程安全的。有关详细信息,请参阅 Swing 的线程策略

Warning: 此类的序列化对象将与未来的 Swing 版本不兼容。当前的序列化支持适用于运行相同版本 Swing 的应用程序之间的短期存储或 RMI。从 1.4 开始,对所有 JavaBeans 的长期存储的支持已添加到 java.beans 包中。请参阅 XMLEncoder

自从:
1.2
参见:
  • 字段详细信息

  • 构造方法详细信息

    • JMenu

      public JMenu()
      构造一个没有文本的新 JMenu
    • JMenu

      public JMenu(String  s)
      使用提供的字符串作为其文本构造一个新的 JMenu
      参数:
      s - 菜单标签的文本
    • JMenu

      public JMenu(Action  a)
      构造一个菜单,其属性取自提供的Action
      参数:
      a - 一个 Action
      自从:
      1.3
    • JMenu

      public JMenu(String  s, boolean b)
      构造一个新的 JMenu,将提供的字符串作为其文本,并指定是否为可撕下的菜单。
      参数:
      s - 菜单标签的文本
      b-菜单是否可以撕掉(暂未实现)
  • 方法详情

    • updateUI

      public void updateUI()
      使用当前外观的值重置 UI 属性。
      重写:
      updateUI 在类 JMenuItem
      参见:
    • getUIClassID

      @BeanProperty (bound =false) public String  getUIClassID()
      返回呈现此组件的 L&F 类的名称。
      重写:
      getUIClassID 在类 JMenuItem
      返回:
      字符串“MenuUI”
      参见:
    • setModel

      public void setModel(ButtonModel  newModel)
      设置“菜单按钮”的数据模型——用户单击以打开或关闭菜单的标签。
      重写:
      setModel 在类 AbstractButton
      参数:
      newModel - ButtonModel
      参见:
    • isSelected

      public boolean isSelected()
      如果当前选择了菜单(突出显示),则返回 true。
      重写:
      isSelected 在类 AbstractButton
      返回:
      如果选择了菜单,则为 true,否则为 false
    • setSelected

      @BeanProperty (expert =true, hidden =true, description ="When the menu is selected, its popup child is shown.") public void setSelected(boolean b)
      设置菜单的选择状态。
      重写:
      setSelected 在类 AbstractButton
      参数:
      b - true 选择(突出显示)菜单; false 取消选择菜单
    • isPopupMenuVisible

      public boolean isPopupMenuVisible()
      如果菜单的弹出窗口可见,则返回 true。
      返回:
      如果菜单可见,则为 true,否则为 false
    • setPopupMenuVisible

      @BeanProperty (bound =false, expert =true, hidden =true, description ="The popup menu\'s visibility") public void setPopupMenuVisible(boolean b)
      设置菜单弹出窗口的可见性。如果菜单未启用,此方法将无效。
      参数:
      b - 一个boolean——true 使菜单可见,false 隐藏它
    • getPopupMenuOrigin

      protected Point  getPopupMenuOrigin()
      计算 JMenu 的弹出菜单的原点。此方法使用名为 Menu.menuPopupOffsetXMenu.menuPopupOffsetYMenu.submenuPopupOffsetXMenu.submenuPopupOffsetY 的外观属性来调整弹出窗口的确切位置。
      返回:
      菜单坐标空间中的 Point 应用作 JMenu 弹出菜单的原点
      自从:
      1.3
    • getDelay

      public int getDelay()
      返回建议的延迟,以毫秒为单位,在子菜单弹出或向下弹出之前。每个外观 (L&F) 都可以确定自己的观察 delay 属性的策略。在大多数情况下,顶级菜单或拖动时不会观察到延迟。 delay 的默认值为 0。此方法是外观代码的一个属性,用于管理各种 UI 实现的特性。
      返回:
      delay 属性
    • setDelay

      @BeanProperty (bound =false, expert =true, description ="The delay between menu selection and making the popup menu visible") public void setDelay(int d)
      在菜单的 PopupMenu 向上或向下弹出之前设置建议的延迟。每个外观 (L&F) 都可以确定它自己的观察延迟属性的策略。在大多数情况下,顶级菜单或拖动时不会观察到延迟。此方法是外观代码的属性,用于管理各种 UI 实现的特性。
      参数:
      d - 延迟的毫秒数
      抛出:
      IllegalArgumentException - 如果 d 小于 0
    • setMenuLocation

      public void setMenuLocation(int x, int y)
      设置弹出组件的位置。
      参数:
      x - 弹出窗口新位置的 x 坐标
      y - 弹出窗口新位置的 y 坐标
    • add

      public JMenuItem  add(JMenuItem  menuItem)
      将菜单项附加到此菜单的末尾。返回添加的菜单项。
      参数:
      menuItem - 要添加的 JMenuitem
      返回:
      JMenuItem 添加
    • add

      public Component  add(Component  c)
      将组件附加到此菜单的末尾。返回添加的组件。
      重写:
      add 在类 Container
      参数:
      c - 要添加的 Component
      返回:
      Component 添加
      参见:
    • add

      public Component  add(Component  c, int index)
      将指定的组件添加到此容器的给定位置。如果 index 等于 -1,则组件将附加到末尾。
      重写:
      add 在类 Container
      参数:
      c - 要添加的 Component
      index - 插入元件的位置
      返回:
      Component 添加
      参见:
    • add

      public JMenuItem  add(String  s)
      创建具有指定文本的新菜单项并将其附加到此菜单的末尾。
      参数:
      s - 要添加的菜单项的字符串
      返回:
      新的JMenuItem
    • add

      public JMenuItem  add(Action  a)
      创建附加到指定 Action 对象的新菜单项,并将其附加到此菜单的末尾。
      参数:
      a - 要添加的菜单项的 Action
      返回:
      新的JMenuItem
      参见:
    • createActionComponent

      protected JMenuItem  createActionComponent(Action  a)
      为添加到 JMenuAction 创建 JMenuItem 的工厂方法。
      参数:
      a - 要添加的菜单项的 Action
      返回:
      新菜单项
      自从:
      1.3
      参见:
    • createActionChangeListener

      protected PropertyChangeListener  createActionChangeListener(JMenuItem  b)
      返回正确配置的 PropertyChangeListener,它会在 Action 发生更改时更新控件。
      参数:
      b - 为其创建 PropertyChangeListener 的菜单项
      返回:
      PropertyChangeListener 用于 b
    • addSeparator

      public void addSeparator()
      将新的分隔符附加到菜单的末尾。
    • insert

      public void insert(String  s, int pos)
      在给定位置插入具有指定文本的新菜单项。
      参数:
      s - 要添加的菜单项的文本
      pos - 一个整数,指定添加新菜单项的位置
      抛出:
      IllegalArgumentException - 当 pos 的值 < 0
    • insert

      public JMenuItem  insert(JMenuItem  mi, int pos)
      在给定位置插入指定的 JMenuitem
      参数:
      mi - 要添加的 JMenuitem
      pos - 一个整数,指定添加新 JMenuitem 的位置
      返回:
      新菜单项
      抛出:
      IllegalArgumentException - 如果 pos 的值 < 0
    • insert

      public JMenuItem  insert(Action  a, int pos)
      在给定位置插入附加到指定 Action 对象的新菜单项。
      参数:
      a - 要添加的菜单项的 Action 对象
      pos - 一个整数,指定添加新菜单项的位置
      返回:
      新菜单项
      抛出:
      IllegalArgumentException - 如果 pos 的值 < 0
    • insertSeparator

      public void insertSeparator(int index)
      在指定位置插入分隔符。
      参数:
      index - 一个整数,指定插入菜单分隔符的位置
      抛出:
      IllegalArgumentException - 如果 index 的值 < 0
    • getItem

      public JMenuItem  getItem(int pos)
      返回指定位置的 JMenuItem。如果 pos 处的组件不是菜单项,则返回 null。包含此方法是为了与 AWT 兼容。
      参数:
      pos - 指定位置的整数
      返回:
      指定位置的菜单项;或 null 如果作为指定位置的项目不是菜单项
      抛出:
      IllegalArgumentException - 如果 pos 的值 < 0
    • getItemCount

      @BeanProperty (bound =false) public int getItemCount()
      返回菜单上的项目数,包括分隔符。包含此方法是为了与 AWT 兼容。
      返回:
      等于菜单上项目数的整数
      参见:
    • isTearOff

      @BeanProperty (bound =false) public boolean isTearOff()
      如果菜单可以撕掉,则返回真。此方法尚未实现。
      返回:
      如果菜单可以撕下则为 true,否则为 false
      抛出:
      Error - 如果调用 - 此方法尚未实现
    • remove

      public void remove(JMenuItem  item)
      从此菜单中删除指定的菜单项。如果没有弹出菜单,则此方法无效。
      参数:
      item - 要从菜单中删除的 JMenuItem
    • remove

      public void remove(int pos)
      从此菜单中删除指定索引处的菜单项。
      重写:
      remove 在类 Container
      参数:
      pos - 要删除的项目的位置
      抛出:
      IllegalArgumentException - 如果 pos 的值 < 0,或者如果 pos 大于菜单项的数量
      参见:
    • remove

      public void remove(Component  c)
      从此菜单中删除组件 c
      重写:
      remove 在类 Container
      参数:
      c - 要删除的组件
      参见:
    • removeAll

      public void removeAll()
      从此菜单中删除所有菜单项。
      重写:
      removeAll 在类 Container
      参见:
    • getMenuComponentCount

      @BeanProperty (bound =false) public int getMenuComponentCount()
      返回菜单上的组件数。
      返回:
      包含菜单上组件数的整数
    • getMenuComponent

      public Component  getMenuComponent(int n)
      返回位置 n 处的组件。
      参数:
      n - 要返回的组件的位置
      返回:
      请求的组件,或者 null 如果没有弹出菜单
    • getMenuComponents

      @BeanProperty (bound =false) public Component [] getMenuComponents()
      返回菜单子组件的 Component 数组。请注意,这将返回弹出菜单中的所有 Component ,包括分隔符。
      返回:
      如果没有弹出菜单,则为 Component 数组或空数组
    • isTopLevelMenu

      @BeanProperty (bound =false) public boolean isTopLevelMenu()
      如果菜单是“顶级菜单”,即如果它是菜单栏的直接子项,则返回 true。
      返回:
      如果菜单是从菜单栏激活的,则为 true;如果菜单是从另一个菜单上的菜单项激活的,则为 false
    • isMenuComponent

      public boolean isMenuComponent(Component  c)
      如果指定的组件存在于子菜单层次结构中,则返回 true。
      参数:
      c - 要测试的 Component
      返回:
      如果 Component 存在则为真,否则为假
    • getPopupMenu

      @BeanProperty (bound =false) public JPopupMenu  getPopupMenu()
      返回与此菜单关联的弹出菜单。如果没有弹出菜单,它将创建一个。
      返回:
      与此菜单关联的 JPopupMenu
    • addMenuListener

      public void addMenuListener(MenuListener  l)
      为菜单事件添加一个监听。
      参数:
      l - 要添加的监听器
    • removeMenuListener

      public void removeMenuListener(MenuListener  l)
      删除菜单事件的监听。
      参数:
      l - 要删除的监听器
    • getMenuListeners

      @BeanProperty (bound =false) public MenuListener [] getMenuListeners()
      返回使用 addMenuListener() 添加到此 JMenu 的所有 MenuListener 的数组。
      返回:
      添加所有 MenuListener s,如果没有添加任何监听器,则为空数组
      自从:
      1.4
    • fireMenuSelected

      protected void fireMenuSelected()
      通知所有已注册对此事件类型的通知感兴趣的监听。事件实例是延迟创建的。
      抛出:
      Error - 如果有一个 null 监听器
      参见:
    • fireMenuDeselected

      protected void fireMenuDeselected()
      通知所有已注册对此事件类型的通知感兴趣的监听。事件实例是延迟创建的。
      抛出:
      Error - 如果有一个 null 监听器
      参见:
    • fireMenuCanceled

      protected void fireMenuCanceled()
      通知所有已注册对此事件类型的通知感兴趣的监听。事件实例是延迟创建的。
      抛出:
      Error - 如果有一个 null 监听器
      参见:
    • createWinListener

      protected JMenu.WinListener  createWinListener(JPopupMenu  p)
      为弹出窗口创建一个窗口关闭监听。
      参数:
      p - JPopupMenu
      返回:
      新的窗口关闭监听
      参见:
    • getSubElements

      @BeanProperty (bound =false) public MenuElement [] getSubElements()
      返回包含此菜单组件的子菜单的 MenuElement 数组。如果弹出菜单是 null 则返回一个空数组。此方法需要符合 MenuElement 接口。请注意,由于 JSeparator 不符合 MenuElement 接口,因此该数组将仅包含 JMenuItem
      指定者:
      getSubElements 在接口 MenuElement
      重写:
      getSubElements 在类 JMenuItem
      返回:
      MenuElement 对象数组
    • getComponent

      public Component  getComponent()
      返回用于绘制此 MenuElementjava.awt.Component。返回的组件用于转换事件并检测事件是否在菜单组件内。
      指定者:
      getComponent 在接口 MenuElement
      重写:
      getComponent 在类 JMenuItem
      返回:
      绘制此菜单项的 Component
    • applyComponentOrientation

      public void applyComponentOrientation(ComponentOrientation  o)
      设置此菜单及其中包含的所有组件的 ComponentOrientation 属性。这包括 getMenuComponents 返回的所有组件。
      重写:
      applyComponentOrientation 在类 Container
      参数:
      o - 此菜单及其中包含的组件的新组件方向。
      抛出:
      NullPointerException - 如果 orientation 为空。
      自从:
      1.4
      参见:
    • setComponentOrientation

      public void setComponentOrientation(ComponentOrientation  o)
      设置此菜单的方向及其由 ComponentOrientation 参数确定的关联弹出菜单。
      重写:
      setComponentOrientation 在类 Component
      参数:
      o - 此菜单及其相关弹出菜单的新方向。
      参见:
    • setAccelerator

      public void setAccelerator(KeyStroke  keyStroke)
      setAccelerator 没有为 JMenu 定义。请改用 setMnemonic
      重写:
      setAccelerator 在类 JMenuItem
      参数:
      keyStroke - 击键组合将调用 JMenuItem 的 actionlisteners 而无需导航菜单层次结构
      抛出:
      Error - 如果调用 - 此方法未为 JMenu 定义。使用 setMnemonic 代替
    • processKeyEvent

      protected void processKeyEvent(KeyEvent  evt)
      处理击键事件,例如助记符和加速器。
      重写:
      processKeyEvent 在类 JComponent
      参数:
      evt - 要处理的按键事件
      参见:
    • doClick

      public void doClick(int pressTime)
      以编程方式执行“点击”。这会覆盖方法 AbstractButton.doClick 以使菜单弹出。
      重写:
      doClick 在类 AbstractButton
      参数:
      pressTime - 指示按下按钮的毫秒数
    • paramString

      protected String  paramString()
      返回此 JMenu 的字符串表示形式。此方法仅用于调试目的,返回字符串的内容和格式可能因实现而异。返回的字符串可能为空,但可能不是 null
      重写:
      paramString 在类 JMenuItem
      返回:
      此 JMenu 的字符串表示形式。
    • getAccessibleContext

      @BeanProperty (bound =false) public AccessibleContext  getAccessibleContext()
      获取与此 JMenu 关联的 AccessibleContext。对于 JMenus,AccessibleContext 采用 AccessibleJMenu 的形式。如有必要,将创建一个新的 AccessibleJMenu 实例。
      指定者:
      getAccessibleContext 在接口 Accessible
      重写:
      getAccessibleContext 在类 JMenuItem
      返回:
      作为此 JMenu 的 AccessibleContext 的 AccessibleJMenu