java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.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
-
内部类总结
内部类修饰符和类型类描述protected class此类实现了对JPopupMenu类的辅助功能支持。static class特定于弹出菜单的分隔符。在类 javax.swing.JComponent 中声明的嵌套类/接口
JComponent.AccessibleJComponent在类 java.awt.Container 中声明的嵌套类/接口
Container.AccessibleAWTContainer -
字段摘要
在类 javax.swing.JComponent 中声明的字段
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW在类 java.awt.Component 中声明的字段
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT在接口 java.awt.image.ImageObserver 中声明的字段
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH -
构造方法总结
构造方法 -
方法总结
修饰符和类型方法描述创建具有指定文本的新菜单项并将其附加到此菜单的末尾。将一个新的菜单项附加到调度指定的Action对象的菜单末尾。将指定的菜单项附加到此菜单的末尾。void添加一个MenuKeyListener到弹出菜单。void添加一个PopupMenu监听器。void在菜单末尾附加一个新的分隔符。protected PropertyChangeListener返回正确配置的PropertyChangeListener,它会在Action发生更改时更新控件。protected JMenuItem为添加到JPopupMenu的Actions创建JMenuItem的工厂方法。protected void通知PopupMenuListeners此弹出菜单已取消。protected void通知PopupMenuListeners 该弹出菜单将变得不可见。protected void通知PopupMenuListeners 此弹出菜单将变为可见。获取与此 JPopupMenu 关联的 AccessibleContext。返回此JPopupMenu组件。getComponentAtIndex(int i) 已弃用。int返回指定组件的索引。static boolean获取defaultLightWeightPopupEnabled属性,默认情况下为true。返回作为此弹出菜单的“调用者”的组件。getLabel()返回弹出菜单的标签返回弹出菜单的边框与其容器之间的边距(以像素为单位)。返回使用 addMenuKeyListener() 添加到此 JPopupMenu 的所有MenuKeyListener的数组。返回使用 addPopupMenuListener() 添加到此 JMenuItem 的所有PopupMenuListener的数组。返回处理单个选择的模型对象。返回包含此菜单组件的子菜单的MenuElement数组。getUI()返回呈现此组件的外观 (L&F) 对象。返回呈现此组件的 L&F 类的名称。void将指定的组件插入到菜单中的给定位置。void在给定位置为指定的Action对象插入一个菜单项。boolean检查是否应绘制边框。boolean获取lightWeightPopupEnabled属性。boolean如果MouseEvent被JPopupMenu当前安装的 UI 视为弹出触发器,则返回 true。boolean如果弹出菜单可见(当前正在显示),则返回 true。voidmenuSelectionChanged(boolean isIncluded) 当菜单栏选择更改为激活或停用此菜单时发出消息。voidpack()布置容器,使其使用显示其内容所需的最小空间。protected void如果borderPainted属性为true,则绘制弹出菜单的边框。protected String返回此JPopupMenu的字符串表示形式protected voidprocessKeyEvent(KeyEvent evt) 处理击键事件,例如助记符和加速器。voidprocessKeyEvent(KeyEvent e, MenuElement[] path, MenuSelectionManager manager) 处理从MenuSelectionManager转发的按键事件,并在必要时使用MenuSelectionManager的 API 更改菜单选择。voidprocessMouseEvent(MouseEvent event, MenuElement[] path, MenuSelectionManager manager) 此方法要求符合MenuElement接口,但未实现。voidremove(int pos) 从此弹出菜单中移除指定索引处的组件。void从弹出菜单中删除MenuKeyListener。void删除PopupMenu监听器。voidsetBorderPainted(boolean b) 设置是否应绘制边框。static voidsetDefaultLightWeightPopupEnabled(boolean aFlag) 设置lightWeightPopupEnabled属性的默认值。voidsetInvoker(Component invoker) 设置此弹出菜单的调用程序——要在其中显示弹出菜单菜单的组件。void设置弹出菜单的标签。voidsetLightWeightPopupEnabled(boolean aFlag) 设置lightWeightPopupEnabled属性的值,默认情况下为true。voidsetLocation(int x, int y) 使用 x、y 坐标设置弹出菜单左上角的位置。voidsetPopupSize(int width, int height) 将弹出窗口的大小设置为指定的宽度和高度。void使用Dimension对象设置弹出窗口的大小。voidsetSelected(Component sel) 设置当前选择的组件,这将导致选择模型发生变化。void设置模型对象以处理单个选择。voidsetUI(PopupMenuUI ui) 设置呈现此组件的 L&F 对象。voidsetVisible(boolean b) 设置弹出菜单的可见性。void在组件调用程序的坐标空间中的 x,y 位置显示弹出菜单。voidupdateUI()将 UI 属性重置为当前外观的值。在类 javax.swing.JComponent 中声明的方法
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, unregisterKeyboardAction, update在类 java.awt.Container 中声明的方法
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree在类 java.awt.Component 中声明的方法
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setMixingCutoutShape, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
-
构造方法详细信息
-
JPopupMenu
public JPopupMenu()构造一个没有“调用者”的JPopupMenu。 -
JPopupMenu
构造具有指定标题的JPopupMenu。- 参数:
label- UI 可用于显示为弹出菜单标题的字符串。
-
-
方法详情
-
setDefaultLightWeightPopupEnabled
public static void setDefaultLightWeightPopupEnabled(boolean aFlag) 设置lightWeightPopupEnabled属性的默认值。- 参数:
aFlag-true如果弹出窗口可以是轻量级的,否则false- 参见:
-
getDefaultLightWeightPopupEnabled
public static boolean getDefaultLightWeightPopupEnabled()获取defaultLightWeightPopupEnabled属性,默认情况下为true。- 返回:
defaultLightWeightPopupEnabled属性的值- 参见:
-
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- 新的PopupMenuUIL&F 对象- 参见:
-
updateUI
public void updateUI()将 UI 属性重置为当前外观的值。- 重写:
updateUI在类JComponent中- 参见:
-
getUIClassID
返回呈现此组件的 L&F 类的名称。- 重写:
getUIClassID在类JComponent中- 返回:
- 字符串“PopupMenuUI”
- 参见:
-
processKeyEvent
处理击键事件,例如助记符和加速器。- 重写:
processKeyEvent在类JComponent中- 参数:
evt- 要处理的按键事件- 参见:
-
getSelectionModel
返回处理单个选择的模型对象。- 返回:
selectionModel属性- 参见:
-
setSelectionModel
@BeanProperty (bound =false, expert =true, description ="The selection model for the popup menu") public void setSelectionModel(SingleSelectionModel model) 设置模型对象以处理单个选择。- 参数:
model- 新的SingleSelectionModel- 参见:
-
add
将指定的菜单项附加到此菜单的末尾。- 参数:
menuItem- 要添加的JMenuItem- 返回:
JMenuItem添加
-
add
创建具有指定文本的新菜单项并将其附加到此菜单的末尾。- 参数:
s- 要添加的菜单项的字符串- 返回:
-
使用
s创建的新JMenuItem
-
add
将一个新的菜单项附加到调度指定的Action对象的菜单末尾。- 参数:
a- 添加到菜单的Action- 返回:
- 新菜单项
- 参见:
-
createActionComponent
为添加到JPopupMenu的Actions创建JMenuItem的工厂方法。- 参数:
a- 要添加的菜单项的Action- 返回:
- 新菜单项
- 自从:
- 1.3
- 参见:
-
createActionChangeListener
返回正确配置的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
返回弹出菜单的标签- 返回:
- 包含弹出菜单标签的字符串
- 参见:
-
setLabel
设置弹出菜单的标签。不同的观感可以选择显示或不显示这个。- 参数:
label- 指定弹出菜单标签的字符串- 参见:
-
addSeparator
public void addSeparator()在菜单末尾附加一个新的分隔符。 -
insert
在给定位置为指定的Action对象插入一个菜单项。- 参数:
a- 要插入的Action对象index- 指定插入Action的位置,其中 0 是第一个- 抛出:
IllegalArgumentException- 如果index< 0- 参见:
-
insert
将指定的组件插入到菜单中的给定位置。- 参数:
component- 要插入的Componentindex- 指定插入组件的位置,其中 0 是第一个- 抛出:
IllegalArgumentException- 如果index< 0
-
addPopupMenuListener
添加一个PopupMenu监听器。- 参数:
l- 要添加的PopupMenuListener
-
removePopupMenuListener
删除PopupMenu监听器。- 参数:
l- 要删除的PopupMenuListener
-
getPopupMenuListeners
返回使用 addPopupMenuListener() 添加到此 JMenuItem 的所有PopupMenuListener的数组。- 返回:
-
添加所有
PopupMenuListener或如果未添加任何监听器,则为空数组 - 自从:
- 1.4
-
addMenuKeyListener
添加一个MenuKeyListener到弹出菜单。- 参数:
l- 要添加的MenuKeyListener- 自从:
- 1.5
-
removeMenuKeyListener
从弹出菜单中删除MenuKeyListener。- 参数:
l- 要删除的MenuKeyListener- 自从:
- 1.5
-
getMenuKeyListeners
返回使用 addMenuKeyListener() 添加到此 JPopupMenu 的所有MenuKeyListener的数组。- 返回:
-
添加所有
MenuKeyListener或如果未添加任何监听器,则为空数组 - 自从:
- 1.5
-
firePopupMenuWillBecomeVisible
protected void firePopupMenuWillBecomeVisible()通知PopupMenuListeners 此弹出菜单将变为可见。 -
firePopupMenuWillBecomeInvisible
protected void firePopupMenuWillBecomeInvisible()通知PopupMenuListeners 该弹出菜单将变得不可见。 -
firePopupMenuCanceled
protected void firePopupMenuCanceled()通知PopupMenuListeners此弹出菜单已取消。 -
pack
public void pack()布置容器,使其使用显示其内容所需的最小空间。 -
setVisible
设置弹出菜单的可见性。- 重写:
setVisible在类JComponent中- 参数:
b- true 使弹出窗口可见,或 false 隐藏它- 参见:
-
isVisible
public boolean isVisible()如果弹出菜单可见(当前正在显示),则返回 true。 -
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
返回作为此弹出菜单的“调用者”的组件。- 返回:
-
显示弹出菜单的
Component
-
setInvoker
@BeanProperty (bound =false, expert =true, description ="The invoking component for the popup menu") public void setInvoker(Component invoker) 设置此弹出菜单的调用程序——要在其中显示弹出菜单菜单的组件。- 参数:
invoker- 显示弹出菜单的Component
-
show
在组件调用程序的坐标空间中的 x,y 位置显示弹出菜单。- 参数:
invoker- 弹出菜单将出现在其空间中的组件x- 显示弹出菜单的调用者坐标空间中的 x 坐标y- 显示弹出菜单的调用者坐标空间中的 y 坐标
-
getComponentAtIndex
已弃用。返回指定索引处的组件。- 参数:
i- 组件的索引,其中 0 是第一个- 返回:
-
该索引处的
Component
-
getComponentIndex
返回指定组件的索引。- 参数:
c- 要查找的Component- 返回:
- 组件的索引,其中 0 是第一个;如果未找到该组件,则为 -1
-
setPopupSize
使用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
如果borderPainted属性为true,则绘制弹出菜单的边框。- 重写:
paintBorder在类JComponent中- 参数:
g-Graphics对象- 参见:
-
getMargin
返回弹出菜单的边框与其容器之间的边距(以像素为单位)。- 返回:
-
包含边距值的
Insets对象。
-
paramString
返回此JPopupMenu的字符串表示形式。此方法仅用于调试目的,返回字符串的内容和格式可能因实现而异。返回的字符串可能为空,但可能不是null。- 重写:
paramString在类JComponent中- 返回:
-
这个
JPopupMenu的字符串表示。
-
getAccessibleContext
获取与此 JPopupMenu 关联的 AccessibleContext。对于 JPopupMenus,AccessibleContext 采用 AccessibleJPopupMenu 的形式。如有必要,将创建一个新的 AccessibleJPopupMenu 实例。- 指定者:
getAccessibleContext在接口Accessible中- 重写:
getAccessibleContext在类Component中- 返回:
- 作为此 JPopupMenu 的 AccessibleContext 的 AccessibleJPopupMenu
-
processMouseEvent
此方法要求符合MenuElement接口,但未实现。- 指定者:
processMouseEvent在接口MenuElement中- 参数:
event- 待处理的MouseEventpath- 菜单层次结构中接收元素的路径manager- 菜单层次结构的MenuSelectionManager- 参见:
-
processKeyEvent
处理从MenuSelectionManager转发的按键事件,并在必要时使用MenuSelectionManager的 API 更改菜单选择。注意:您不必将事件转发给子组件。这是由
MenuSelectionManager自动完成的。- 指定者:
processKeyEvent在接口MenuElement中- 参数:
e- 一个KeyEventpath-MenuElement路径数组manager-MenuSelectionManager
-
getSubElements
返回包含此菜单组件的子菜单的MenuElement数组。它只会返回符合JMenuElement接口的项目。如果弹出菜单是null则返回一个空数组。此方法需要符合MenuElement接口。- 指定者:
getSubElements在接口MenuElement中- 返回:
MenuElement对象数组- 参见:
-
getComponent
返回此JPopupMenu组件。- 指定者:
getComponent在接口MenuElement中- 返回:
-
这个
JPopupMenu对象 - 参见:
-
isPopupTrigger
如果MouseEvent被JPopupMenu当前安装的 UI 视为弹出触发器,则返回 true。- 参数:
e- 一个MouseEvent- 返回:
- 如果鼠标事件是弹出触发器,则为真
- 自从:
- 1.3
-
Container.getComponent(int)