模块 java.desktop

类 JPopupMenu

所有已实现的接口:
ImageObserver , MenuContainer , Serializable , Accessible , MenuElement
已知子类:
BasicComboPopup

@JavaBean (defaultProperty ="UI", description ="A small window that pops up and displays a series of choices.") public class JPopupMenu extends JComponent implements Accessible , MenuElement
弹出菜单的实现——弹出并显示一系列选项的小窗口。 JPopupMenu 用于当用户选择菜单栏上的项目时出现的菜单。它也用于在选择激活它的菜单项时出现的“向右拉”菜单。最后,JPopupMenu 也可用于您希望菜单出现的任何其他地方。例如,当用户在指定区域单击鼠标右键时。

有关使用弹出菜单的信息和示例,请参阅 The Java Tutorial. 中的 如何使用菜单

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

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

自从:
1.2
  • 构造方法详细信息

    • JPopupMenu

      public JPopupMenu()
      构造一个没有“调用者”的JPopupMenu
    • JPopupMenu

      public JPopupMenu(String  label)
      构造具有指定标题的 JPopupMenu
      参数:
      label - UI 可用于显示为弹出菜单标题的字符串。
  • 方法详情

    • setDefaultLightWeightPopupEnabled

      public static void setDefaultLightWeightPopupEnabled(boolean aFlag)
      设置 lightWeightPopupEnabled 属性的默认值。
      参数:
      aFlag - true 如果弹出窗口可以是轻量级的,否则 false
      参见:
    • getDefaultLightWeightPopupEnabled

      public static boolean getDefaultLightWeightPopupEnabled()
      获取 defaultLightWeightPopupEnabled 属性,默认情况下为 true
      返回:
      defaultLightWeightPopupEnabled 属性的值
      参见:
    • getUI

      public PopupMenuUI  getUI()
      返回呈现此组件的外观 (L&F) 对象。
      重写:
      getUI 在类 JComponent
      返回:
      呈现此组件的 PopupMenuUI 对象
    • setUI

      @BeanProperty (hidden =true, visualUpdate =true, description ="The UI object that implements the Component\'s LookAndFeel.") public void setUI(PopupMenuUI  ui)
      设置呈现此组件的 L&F 对象。
      参数:
      ui - 新的 PopupMenuUI L&F 对象
      参见:
    • updateUI

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

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

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

      public SingleSelectionModel  getSelectionModel()
      返回处理单个选择的模型对象。
      返回:
      selectionModel 属性
      参见:
    • setSelectionModel

      @BeanProperty (bound =false, expert =true, description ="The selection model for the popup menu") public void setSelectionModel(SingleSelectionModel  model)
      设置模型对象以处理单个选择。
      参数:
      model - 新的 SingleSelectionModel
      参见:
    • add

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

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

      public JMenuItem  add(Action  a)
      将一个新的菜单项附加到调度指定的 Action 对象的菜单末尾。
      参数:
      a - 添加到菜单的 Action
      返回:
      新菜单项
      参见:
    • createActionComponent

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

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

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

      @BeanProperty (bound =false, expert =true, description ="Determines whether lightweight popups are used when possible") public void setLightWeightPopupEnabled(boolean aFlag)
      设置 lightWeightPopupEnabled 属性的值,默认情况下为 true 。默认情况下,当外观显示弹出窗口时,它可以选择使用轻量级(全 Java)弹出窗口。轻量级弹出窗口比重量级(本机对等)窗口更有效,但轻量级和重量级组件在 GUI 中不能很好地混合。如果您的应用程序混合了轻量级和重量级组件,您应该禁用轻量级弹出窗口。某些外观可能总是使用重量级弹出窗口,无论此属性的值是多少。
      参数:
      aFlag - false 禁用轻量级弹出窗口
      参见:
    • isLightWeightPopupEnabled

      public boolean isLightWeightPopupEnabled()
      获取 lightWeightPopupEnabled 属性。
      返回:
      lightWeightPopupEnabled 属性的值
      参见:
    • getLabel

      public String  getLabel()
      返回弹出菜单的标签
      返回:
      包含弹出菜单标签的字符串
      参见:
    • setLabel

      @BeanProperty (description ="The label for the popup menu.") public void setLabel(String  label)
      设置弹出菜单的标签。不同的观感可以选择显示或不显示这个。
      参数:
      label - 指定弹出菜单标签的字符串
      参见:
    • addSeparator

      public void addSeparator()
      在菜单末尾附加一个新的分隔符。
    • insert

      public void insert(Action  a, int index)
      在给定位置为指定的 Action 对象插入一个菜单项。
      参数:
      a - 要插入的 Action 对象
      index - 指定插入 Action 的位置,其中 0 是第一个
      抛出:
      IllegalArgumentException - 如果 index < 0
      参见:
    • insert

      public void insert(Component  component, int index)
      将指定的组件插入到菜单中的给定位置。
      参数:
      component - 要插入的 Component
      index - 指定插入组件的位置,其中 0 是第一个
      抛出:
      IllegalArgumentException - 如果 index < 0
    • addPopupMenuListener

      public void addPopupMenuListener(PopupMenuListener  l)
      添加一个 PopupMenu 监听器。
      参数:
      l - 要添加的 PopupMenuListener
    • removePopupMenuListener

      public void removePopupMenuListener(PopupMenuListener  l)
      删除 PopupMenu 监听器。
      参数:
      l - 要删除的 PopupMenuListener
    • getPopupMenuListeners

      @BeanProperty (bound =false) public PopupMenuListener [] getPopupMenuListeners()
      返回使用 addPopupMenuListener() 添加到此 JMenuItem 的所有 PopupMenuListener 的数组。
      返回:
      添加所有 PopupMenuListener 或如果未添加任何监听器,则为空数组
      自从:
      1.4
    • addMenuKeyListener

      public void addMenuKeyListener(MenuKeyListener  l)
      添加一个 MenuKeyListener 到弹出菜单。
      参数:
      l - 要添加的 MenuKeyListener
      自从:
      1.5
    • removeMenuKeyListener

      public void removeMenuKeyListener(MenuKeyListener  l)
      从弹出菜单中删除 MenuKeyListener
      参数:
      l - 要删除的 MenuKeyListener
      自从:
      1.5
    • getMenuKeyListeners

      @BeanProperty (bound =false) public MenuKeyListener [] getMenuKeyListeners()
      返回使用 addMenuKeyListener() 添加到此 JPopupMenu 的所有 MenuKeyListener 的数组。
      返回:
      添加所有 MenuKeyListener 或如果未添加任何监听器,则为空数组
      自从:
      1.5
    • firePopupMenuWillBecomeVisible

      protected void firePopupMenuWillBecomeVisible()
      通知 PopupMenuListener s 此弹出菜单将变为可见。
    • firePopupMenuWillBecomeInvisible

      protected void firePopupMenuWillBecomeInvisible()
      通知 PopupMenuListener s 该弹出菜单将变得不可见。
    • firePopupMenuCanceled

      protected void firePopupMenuCanceled()
      通知 PopupMenuListeners 此弹出菜单已取消。
    • pack

      public void pack()
      布置容器,使其使用显示其内容所需的最小空间。
    • setVisible

      @BeanProperty (description ="Makes the popup visible") public void setVisible(boolean b)
      设置弹出菜单的可见性。
      重写:
      setVisible 在类 JComponent
      参数:
      b - true 使弹出窗口可见,或 false 隐藏它
      参见:
    • isVisible

      public boolean isVisible()
      如果弹出菜单可见(当前正在显示),则返回 true。
      重写:
      isVisible 在类 Component
      返回:
      true 如果组件可见,false 否则
      参见:
    • setLocation

      @BeanProperty (description ="The location of the popup menu.") public void setLocation(int x, int y)
      使用 x、y 坐标设置弹出菜单左上角的位置。

      该方法更改几何相关数据。因此,本机窗口系统可能会忽略此类请求,或者它可能会修改请求的数据,以便 JPopupMenu 对象的放置和大小与桌面设置密切相关。

      重写:
      setLocation 在类 Component
      参数:
      x - 弹出窗口在屏幕坐标空间中的新位置的 x 坐标
      y - 弹出窗口在屏幕坐标空间中的新位置的 y 坐标
      参见:
    • getInvoker

      public Component  getInvoker()
      返回作为此弹出菜单的“调用者”的组件。
      返回:
      显示弹出菜单的Component
    • setInvoker

      @BeanProperty (bound =false, expert =true, description ="The invoking component for the popup menu") public void setInvoker(Component  invoker)
      设置此弹出菜单的调用程序——要在其中显示弹出菜单菜单的组件。
      参数:
      invoker - 显示弹出菜单的 Component
    • show

      public void show(Component  invoker, int x, int y)
      在组件调用程序的坐标空间中的 x,y 位置显示弹出菜单。
      参数:
      invoker - 弹出菜单将出现在其空间中的组件
      x - 显示弹出菜单的调用者坐标空间中的 x 坐标
      y - 显示弹出菜单的调用者坐标空间中的 y 坐标
    • getComponentAtIndex

      @Deprecated public Component  getComponentAtIndex(int i)
      已弃用。
      返回指定索引处的组件。
      参数:
      i - 组件的索引,其中 0 是第一个
      返回:
      该索引处的 Component
    • getComponentIndex

      public int getComponentIndex(Component  c)
      返回指定组件的索引。
      参数:
      c - 要查找的 Component
      返回:
      组件的索引,其中 0 是第一个;如果未找到该组件,则为 -1
    • setPopupSize

      @BeanProperty (description ="The size of the popup menu") public void setPopupSize(Dimension  d)
      使用 Dimension 对象设置弹出窗口的大小。这相当于 setPreferredSize(d)
      参数:
      d - Dimension 指定此组件的新大小。
    • setPopupSize

      @BeanProperty (description ="The size of the popup menu") public void setPopupSize(int width, int height)
      将弹出窗口的大小设置为指定的宽度和高度。这相当于 setPreferredSize(new Dimension(width, height))
      参数:
      width - Popup 的新宽度(以像素为单位)
      height - Popup 的新高度(以像素为单位)
    • setSelected

      @BeanProperty (expert =true, hidden =true, description ="The selected component on the popup menu") public void setSelected(Component  sel)
      设置当前选择的组件,这将导致选择模型发生变化。
      参数:
      sel - 要选择的 Component
    • isBorderPainted

      public boolean isBorderPainted()
      检查是否应绘制边框。
      返回:
      如果绘制了边框,则为 true,否则为 false
      参见:
    • setBorderPainted

      @BeanProperty (bound =false, description ="Is the border of the popup menu painted") public void setBorderPainted(boolean b)
      设置是否应绘制边框。
      参数:
      b - 如果为真,则绘制边框。
      参见:
    • paintBorder

      protected void paintBorder(Graphics  g)
      如果 borderPainted 属性为 true ,则绘制弹出菜单的边框。
      重写:
      paintBorder 在类 JComponent
      参数:
      g - Graphics 对象
      参见:
    • getMargin

      @BeanProperty (bound =false) public Insets  getMargin()
      返回弹出菜单的边框与其容器之间的边距(以像素为单位)。
      返回:
      包含边距值的 Insets 对象。
    • paramString

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

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

      public void processMouseEvent(MouseEvent  event, MenuElement [] path, MenuSelectionManager  manager)
      此方法要求符合MenuElement接口,但未实现。
      指定者:
      processMouseEvent 在接口 MenuElement
      参数:
      event - 待处理的 MouseEvent
      path - 菜单层次结构中接收元素的路径
      manager - 菜单层次结构的 MenuSelectionManager
      参见:
    • processKeyEvent

      public void processKeyEvent(KeyEvent  e, MenuElement [] path, MenuSelectionManager  manager)
      处理从 MenuSelectionManager 转发的按键事件,并在必要时使用 MenuSelectionManager 的 API 更改菜单选择。

      注意:您不必将事件转发给子组件。这是由 MenuSelectionManager 自动完成的。

      指定者:
      processKeyEvent 在接口 MenuElement
      参数:
      e - 一个 KeyEvent
      path - MenuElement 路径数组
      manager - MenuSelectionManager
    • getSubElements

      @BeanProperty (bound =false) public MenuElement [] getSubElements()
      返回包含此菜单组件的子菜单的 MenuElement 数组。它只会返回符合 JMenuElement 接口的项目。如果弹出菜单是 null 则返回一个空数组。此方法需要符合 MenuElement 接口。
      指定者:
      getSubElements 在接口 MenuElement
      返回:
      MenuElement 对象数组
      参见:
    • getComponent

      public Component  getComponent()
      返回此 JPopupMenu 组件。
      指定者:
      getComponent 在接口 MenuElement
      返回:
      这个 JPopupMenu 对象
      参见:
    • isPopupTrigger

      public boolean isPopupTrigger(MouseEvent  e)
      如果 MouseEventJPopupMenu 当前安装的 UI 视为弹出触发器,则返回 true。
      参数:
      e - 一个 MouseEvent
      返回:
      如果鼠标事件是弹出触发器,则为真
      自从:
      1.3