模块  java.desktop
软件包  javax.swing

Class JPopupMenu

  • 实现的所有接口
    ImageObserverMenuContainerSerializableAccessibleMenuElement
    已知直接子类:
    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中的 How to Use Menus

    警告: Swing不是线程安全的。 有关更多信息,请参阅Swing's Threading Policy

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

    从以下版本开始:
    1.2
    另请参见:
    Serialized Form
    • 构造方法详细信息

      • JPopupMenu

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

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

      • getDefaultLightWeightPopupEnabled

        public static boolean getDefaultLightWeightPopupEnabled()
        获取 defaultLightWeightPopupEnabled属性,默认为 true
        结果
        defaultLightWeightPopupEnabled属性的值
        另请参见:
        setDefaultLightWeightPopupEnabled(boolean)
      • getUI

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

        public JMenuItem add​(JMenuItem menuItem)
        将指定的菜单项追加到此菜单的末尾。
        参数
        menuItem - 要添加的 JMenuItem
        结果
        JMenuItem补充说
      • add

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

        public JMenuItem add​(Action a)
        将新菜单项追加到菜单末尾,该菜单项将调度指定的 Action对象。
        参数
        a - 要添加到菜单中的 Action
        结果
        新菜单项
        另请参见:
        Action
      • createActionComponent

        protected JMenuItem createActionComponent​(Action a)
        工厂方法创建 JMenuItem Actions添加到 JPopupMenu
        参数
        a - 要添加的菜单项的 Action
        结果
        新菜单项
        从以下版本开始:
        1.3
        另请参见:
        Action
      • createActionChangeListener

        protected PropertyChangeListener createActionChangeListener​(JMenuItem b)
        返回正确配置的 PropertyChangeListener ,在更改 Action时更新控件。
        参数
        b - 要为其创建侦听器的菜单项
        结果
        正确配置 PropertyChangeListener
      • 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()
      • isLightWeightPopupEnabled

        public boolean isLightWeightPopupEnabled()
        获取 lightWeightPopupEnabled属性。
        结果
        lightWeightPopupEnabled属性的值
        另请参见:
        setLightWeightPopupEnabled(boolean)
      • setLabel

        @BeanProperty(description="The label for the popup menu.")
        public void setLabel​(String label)
        设置弹出菜单的标签。 不同的外观可能会选择显示或不显示此内容。
        参数
        label - 指定弹出菜单标签的字符串
        另请参见:
        setLabel(java.lang.String)
      • addSeparator

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

        public void insert​(Action a,
                           int index)
        在给定位置插入指定的 Action对象的菜单项。
        参数
        a - 要插入的 Action对象
        index - 指定插入 Action的位置,其中0是第一个
        异常
        IllegalArgumentException - 如果 index <0
        另请参见:
        Action
      • 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()
        放置容器,使其占用显示其内容所需的最小空间。
      • isVisible

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

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

        该方法更改与几何相关的数据。 因此,本机窗口系统可以忽略这些请求,或者它可以修改所请求的数据,以便以与桌面设置紧密对应的方式放置和调整JPopupMenu对象的大小。

        重写:
        setLocationComponent
        参数
        x - 弹出窗口坐标空间中新位置的x坐标
        y - 弹出窗口坐标空间中新位置的y坐标
        另请参见:
        Component.getLocation()Component.setBounds(int, int, int, int)Component.invalidate()
      • 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)
        Deprecated.
        返回指定索引处的组件。
        参数
        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(boolean)
      • setBorderPainted

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

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

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

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

        public void processKeyEvent​(KeyEvent e,
                                    MenuElement[] path,
                                    MenuSelectionManager manager)
        处理从MenuSelectionManager转发的键事件,并根据需要使用MenuSelectionManager的API更改菜单选项。

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

        Specified by:
        processKeyEvent ,界面 MenuElement
        参数
        e - a KeyEvent
        path - MenuElement路径数组
        manager - MenuSelectionManager
      • menuSelectionChanged

        public void menuSelectionChanged​(boolean isIncluded)
        菜单栏选择更改为激活或取消激活此菜单时消息。 这实现了javax.swing.MenuElement接口。 覆盖MenuElement.menuSelectionChanged
        Specified by:
        menuSelectionChanged ,界面 MenuElement
        参数
        isIncluded - 如果此菜单处于活动状态, isIncluded true;否则为false
        另请参见:
        MenuElement.menuSelectionChanged(boolean)
      • getSubElements

        @BeanProperty(bound=false)
        public MenuElement[] getSubElements()
        返回包含此菜单组件子菜单的MenuElement的数组。 它只会返回符合JMenuElement界面的项目。 如果弹出菜单是null返回一个空数组。 此方法必须符合MenuElement接口。
        Specified by:
        getSubElements ,界面 MenuElement
        结果
        一组 MenuElement对象
        另请参见:
        MenuElement.getSubElements()
      • isPopupTrigger

        public boolean isPopupTrigger​(MouseEvent e)
        如果 MouseEvent被认为是 JPopupMenu当前安装的UI的弹出触发器,则返回true。
        参数
        e - a MouseEvent
        结果
        如果鼠标事件是弹出触发器,则为true
        从以下版本开始:
        1.3