java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.AbstractButton
javax.swing.JMenuItem
- 所有已实现的接口:
-
ImageObserver
,ItemSelectable
,MenuContainer
,Serializable
,Accessible
,MenuElement
,SwingConstants
- 直接已知的子类:
-
JCheckBoxMenuItem
,JMenu
,JRadioButtonMenuItem
@JavaBean(defaultProperty="UIClassID", description="An item which can be selected in a menu.") public class JMenuItem extends AbstractButton implements Accessible, MenuElement
菜单中的一个项目的实现。菜单项本质上是一个位于列表中的按钮。当用户选择“按钮”时,与菜单项关联的操作将被执行。包含在
JPopupMenu
中的JMenuItem
正是执行这个功能。
菜单项可以通过Action
进行配置,并在一定程度上进行控制。使用Action
与菜单项相比,有许多好处。有关更多详细信息,请参阅支持Action
的Swing组件,您可以在The Java Tutorial的如何使用Actions部分找到更多信息。
有关更多文档和示例,请参阅The Java Tutorial中的如何使用菜单。
警告: Swing不是线程安全的。有关更多信息,请参阅Swing的线程策略。
警告: 该类的序列化对象将不兼容未来的Swing版本。当前的序列化支持适用于短期存储或在运行相同Swing版本的应用程序之间进行RMI。从1.4开始,已将所有JavaBeans的长期存储支持添加到java.beans
包中。请参阅XMLEncoder
。
- 自1.2版本起:
- 无
- 参见:
-
Nested Class Summary
Modifier and TypeClassDescriptionprotected class
该类实现了JMenuItem
类的辅助功能支持。Nested classes/interfaces declared in class javax.swing.AbstractButton
AbstractButton.AccessibleAbstractButton, AbstractButton.ButtonChangeListener
Nested classes/interfaces declared in class javax.swing.JComponent
JComponent.AccessibleJComponent
Nested classes/interfaces declared in class java.awt.Container
Container.AccessibleAWTContainer
Nested classes/interfaces declared in class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
-
Field Summary
Fields declared in class javax.swing.AbstractButton
actionListener, BORDER_PAINTED_CHANGED_PROPERTY, changeEvent, changeListener, CONTENT_AREA_FILLED_CHANGED_PROPERTY, DISABLED_ICON_CHANGED_PROPERTY, DISABLED_SELECTED_ICON_CHANGED_PROPERTY, FOCUS_PAINTED_CHANGED_PROPERTY, HORIZONTAL_ALIGNMENT_CHANGED_PROPERTY, HORIZONTAL_TEXT_POSITION_CHANGED_PROPERTY, ICON_CHANGED_PROPERTY, itemListener, MARGIN_CHANGED_PROPERTY, MNEMONIC_CHANGED_PROPERTY, model, MODEL_CHANGED_PROPERTY, PRESSED_ICON_CHANGED_PROPERTY, ROLLOVER_ENABLED_CHANGED_PROPERTY, ROLLOVER_ICON_CHANGED_PROPERTY, ROLLOVER_SELECTED_ICON_CHANGED_PROPERTY, SELECTED_ICON_CHANGED_PROPERTY, TEXT_CHANGED_PROPERTY, VERTICAL_ALIGNMENT_CHANGED_PROPERTY, VERTICAL_TEXT_POSITION_CHANGED_PROPERTY
Fields declared in class javax.swing.JComponent
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
Fields declared in class java.awt.Component
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
Fields declared in interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
Fields declared in interface javax.swing.SwingConstants
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST
-
Constructor Summary
ConstructorDescription创建一个没有设置文本或图标的JMenuItem
。创建一个带有指定文本的JMenuItem
。创建一个带有指定文本和键盘助记符的JMenuItem
。创建一个带有指定文本和图标的JMenuItem
。创建一个从指定Action
中获取属性的菜单项。创建一个带有指定图标的JMenuItem
。 -
Method Summary
Modifier and TypeMethodDescriptionprotected void
actionPropertyChanged
(Action action, String propertyName) 根据关联的操作中的属性更改更新按钮的状态。void
向菜单项添加一个MenuDragMouseListener
。void
向菜单项添加一个MenuKeyListener
。protected void
将此按钮上的属性设置为与指定Action
中的属性相匹配。protected void
通知所有已注册对此事件类型感兴趣的侦听器。protected void
通知所有已注册对此事件类型感兴趣的侦听器。protected void
通知所有已注册对此事件类型感兴趣的侦听器。protected void
通知所有已注册对此事件类型感兴趣的侦听器。protected void
fireMenuKeyPressed
(MenuKeyEvent event) 通知所有已注册对此事件类型感兴趣的侦听器。protected void
fireMenuKeyReleased
(MenuKeyEvent event) 通知所有已注册对此事件类型感兴趣的侦听器。protected void
fireMenuKeyTyped
(MenuKeyEvent event) 通知所有已注册对此事件类型感兴趣的侦听器。返回作为菜单项加速器的KeyStroke
。返回与此JMenuItem
关联的AccessibleContext
。返回用于绘制此对象的java.awt.Component
。返回添加到此JMenuItem的所有MenuDragMouseListener
的数组。返回添加到此JMenuItem的所有MenuKeyListener
的数组。此方法返回包含此菜单组件的子菜单组件的数组。返回用于构造用于呈现此组件的L&F类名称的后缀。protected void
使用指定的文本和图标初始化菜单项。boolean
isArmed()
返回菜单项是否“激活”。void
menuSelectionChanged
(boolean isIncluded) 当选择或取消选择MenuElement
时,由MenuSelectionManager
调用。protected String
返回此JMenuItem
的字符串表示形式。void
processKeyEvent
(KeyEvent e, MenuElement[] path, MenuSelectionManager manager) 处理从MenuSelectionManager
转发的键事件,并根据需要使用MenuSelectionManager
的API更改菜单选择。void
处理菜单中的鼠标拖动。void
处理菜单中的按键。void
processMouseEvent
(MouseEvent e, MenuElement[] path, MenuSelectionManager manager) 处理从MenuSelectionManager
转发的鼠标事件,并根据需要使用MenuSelectionManager
的API更改菜单选择。void
从菜单项中删除一个MenuDragMouseListener
。void
从菜单项中删除一个MenuKeyListener
。void
setAccelerator
(KeyStroke keyStroke) 设置调用菜单项的动作侦听器而无需导航菜单层次结构的键组合。void
setArmed
(boolean b) 将菜单项标识为“激活”。void
setEnabled
(boolean b) 启用或禁用菜单项。void
setModel
(ButtonModel newModel) 设置此按钮表示的模型。void
setUI
(MenuItemUI ui) 设置呈现此组件的外观对象。void
updateUI()
重置来自当前外观的值的UI属性。Methods declared in class javax.swing.AbstractButton
addActionListener, addChangeListener, addImpl, addItemListener, checkHorizontalKey, checkVerticalKey, createActionListener, createActionPropertyChangeListener, createChangeListener, createItemListener, doClick, doClick, fireActionPerformed, fireItemStateChanged, fireStateChanged, getAction, getActionCommand, getActionListeners, getChangeListeners, getDisabledIcon, getDisabledSelectedIcon, getDisplayedMnemonicIndex, getHideActionText, getHorizontalAlignment, getHorizontalTextPosition, getIcon, getIconTextGap, getItemListeners, getLabel, getMargin, getMnemonic, getModel, getMultiClickThreshhold, getPressedIcon, getRolloverIcon, getRolloverSelectedIcon, getSelectedIcon, getSelectedObjects, getText, getUI, getVerticalAlignment, getVerticalTextPosition, imageUpdate, isBorderPainted, isContentAreaFilled, isFocusPainted, isRolloverEnabled, isSelected, paintBorder, removeActionListener, removeChangeListener, removeItemListener, removeNotify, setAction, setActionCommand, setBorderPainted, setContentAreaFilled, setDisabledIcon, setDisabledSelectedIcon, setDisplayedMnemonicIndex, setFocusPainted, setHideActionText, setHorizontalAlignment, setHorizontalTextPosition, setIcon, setIconTextGap, setLabel, setLayout, setMargin, setMnemonic, setMnemonic, setMultiClickThreshhold, setPressedIcon, setRolloverEnabled, setRolloverIcon, setRolloverSelectedIcon, setSelected, setSelectedIcon, setText, setUI, setVerticalAlignment, setVerticalTextPosition
Methods declared in class 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, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
Methods declared in class java.awt.Container
add, add, add, add, add, addContainerListener, 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, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, transferFocusDownCycle, validate, validateTree
Methods declared in class 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, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, 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, setLocation, setMixingCutoutShape, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
-
Constructor Details
-
JMenuItem
public JMenuItem()创建一个没有设置文本或图标的JMenuItem
。 -
JMenuItem
创建一个带有指定图标的JMenuItem
。- 参数:
-
icon
-JMenuItem
的图标
-
JMenuItem
创建一个带有指定文本的JMenuItem
。- 参数:
-
text
-JMenuItem
的文本
-
JMenuItem
创建一个从指定Action
中获取属性的菜单项。- 参数:
-
a
-JMenuItem
的动作 - 自1.3版本起:
- 无
-
JMenuItem
创建一个带有指定文本和图标的JMenuItem
。- 参数:
-
text
-JMenuItem
的文本 -
icon
-JMenuItem
的图标
-
JMenuItem
创建一个带有指定文本和键盘助记符的JMenuItem
。- 参数:
-
text
-JMenuItem
的文本 -
mnemonic
-JMenuItem
的键盘助记符
-
-
Method Details
-
setModel
设置此按钮表示的模型。- 覆盖:
-
setModel
在类AbstractButton
中 - 参数:
-
newModel
- 新的ButtonModel
- 参见:
-
init
使用指定的文本和图标初始化菜单项。- 覆盖:
-
init
在类AbstractButton
中 - 参数:
-
text
-JMenuItem
的文本 -
icon
-JMenuItem
的图标
-
setUI
@BeanProperty(hidden=true, visualUpdate=true, description="The UI object that implements the LookAndFeel.") public void setUI(MenuItemUI ui) 设置呈现此组件的外观对象。- 参数:
-
ui
-JMenuItemUI
的L&F对象 - 参见:
-
updateUI
public void updateUI()重置来自当前外观的值的UI属性。- 覆盖:
-
updateUI
在类AbstractButton
中 - 参见:
-
getUIClassID
返回用于构造用于呈现此组件的L&F类的名称的后缀。- 覆盖:
-
getUIClassID
在类JComponent
中 - 返回:
- 字符串 "MenuItemUI"
- 参见:
-
setArmed
@BeanProperty(bound=false, hidden=true, description="Mouse release will fire an action event") public void setArmed(boolean b) 将菜单项标识为“armed”。如果鼠标按钮在此项上释放,则菜单的操作事件将触发。如果鼠标按钮在其他位置释放,则事件不会触发,菜单项将被解除武装。- 参数:
-
b
- true表示武装菜单项以便选择
-
isArmed
public boolean isArmed()返回菜单项是否“armed”。- 返回:
- 如果菜单项已武装且可选择,则为true
- 参见:
-
setEnabled
@BeanProperty(preferred=true, description="The enabled state of the component.") public void setEnabled(boolean b) 启用或禁用菜单项。- 覆盖:
-
setEnabled
在类AbstractButton
中 - 参数:
-
b
- true表示启用该项 - 参见:
-
setAccelerator
@BeanProperty(preferred=true, description="The keystroke combination which will invoke the JMenuItem\'s actionlisteners without navigating the menu hierarchy") public void setAccelerator(KeyStroke keyStroke) 设置调用菜单项的动作监听器而无需导航菜单层次结构的键组合。安装正确的操作是UI的责任。请注意,当键盘加速键被键入时,无论菜单当前是否显示,都会起作用。- 参数:
-
keyStroke
- 将用作加速器的KeyStroke
-
getAccelerator
返回用作菜单项加速器的KeyStroke
。- 返回:
-
一个标识加速键的
KeyStroke
对象
-
configurePropertiesFromAction
设置此按钮上的属性以匹配指定Action
中的属性。有关此方法设置的属性的更多详细信息,请参阅支持Action
的Swing组件。- 覆盖:
-
configurePropertiesFromAction
在类AbstractButton
中 - 参数:
-
a
- 要获取属性的Action
,或null
- 自:
- 1.3
- 参见:
-
actionPropertyChanged
根据关联动作中的属性更改更新按钮的状态。此方法从createActionPropertyChangeListener
返回的PropertyChangeListener
中调用。子类通常不需要调用此方法。支持其他Action
属性的子类应该重写此方法和configurePropertiesFromAction
。有关此方法设置的属性列表,请参阅支持
Action
的Swing组件中的表格。- 覆盖:
-
actionPropertyChanged
在类AbstractButton
中 - 参数:
-
action
- 与此按钮关联的Action
-
propertyName
- 更改的属性名称 - 自:
- 1.6
- 参见:
-
processMouseEvent
处理从MenuSelectionManager
转发的鼠标事件,并根据需要使用MenuSelectionManager
的API更改菜单选择。注意:您无需将事件转发给子组件。这由
MenuSelectionManager
自动完成。- 指定者:
-
processMouseEvent
在接口MenuElement
中 - 参数:
-
e
- 一个MouseEvent
-
path
-MenuElement
路径数组 -
manager
-MenuSelectionManager
-
processKeyEvent
处理从MenuSelectionManager
转发的键盘事件,并根据需要使用MenuSelectionManager
的API更改菜单选择。注意:您无需将事件转发给子组件。这由
MenuSelectionManager
自动完成。- 指定者:
-
processKeyEvent
在接口MenuElement
中 - 参数:
-
e
- 一个KeyEvent
-
path
-MenuElement
路径数组 -
manager
-MenuSelectionManager
-
processMenuDragMouseEvent
处理菜单中的鼠标拖动。- 参数:
-
e
- 一个MenuDragMouseEvent
对象
-
processMenuKeyEvent
处理菜单中的按键。- 参数:
-
e
- 一个MenuKeyEvent
对象
-
fireMenuDragMouseEntered
通知所有已注册对此事件类型感兴趣的侦听器。- 参数:
-
event
- 一个MenuMouseDragEvent
- 参见:
-
fireMenuDragMouseExited
通知所有已注册对此事件类型感兴趣的侦听器。- 参数:
-
event
- 一个MenuDragMouseEvent
- 参见:
-
fireMenuDragMouseDragged
通知所有已注册对此事件类型感兴趣的侦听器。- 参数:
-
event
- 一个MenuDragMouseEvent
- 参见:
-
fireMenuDragMouseReleased
通知所有已注册对此事件类型感兴趣的侦听器。- 参数:
-
event
- 一个MenuDragMouseEvent
- 参见:
-
fireMenuKeyPressed
通知所有已注册对此事件类型感兴趣的侦听器。- 参数:
-
event
- 一个MenuKeyEvent
- 参见:
-
fireMenuKeyReleased
通知所有已注册对此事件类型感兴趣的侦听器。- 参数:
-
event
- 一个MenuKeyEvent
- 参见:
-
fireMenuKeyTyped
通知所有已注册对此事件类型感兴趣的侦听器。- 参数:
-
event
- 一个MenuKeyEvent
- 参见:
-
getSubElements
此方法返回包含此菜单组件的子菜单组件的数组。- 指定者:
-
getSubElements
在接口MenuElement
中 - 返回:
-
一个
MenuElement
数组
-
getComponent
返回用于绘制此对象的java.awt.Component
。返回的组件将用于转换事件并检测事件是否在菜单组件内部。- 指定者:
-
getComponent
在接口MenuElement
- 返回:
-
绘制此菜单项的
Component
-
addMenuDragMouseListener
向菜单项添加一个MenuDragMouseListener
。- 参数:
-
l
- 要添加的MenuDragMouseListener
-
removeMenuDragMouseListener
从菜单项中删除MenuDragMouseListener
。- 参数:
-
l
- 要移除的MenuDragMouseListener
-
getMenuDragMouseListeners
返回添加到此JMenuItem中的所有MenuDragMouseListener
的数组。- 返回:
-
所有已添加的
MenuDragMouseListener
或如果未添加任何监听器则返回空数组 - 自:
- 1.4
-
addMenuKeyListener
向菜单项添加一个MenuKeyListener
。- 参数:
-
l
- 要添加的MenuKeyListener
-
removeMenuKeyListener
从菜单项中删除MenuKeyListener
。- 参数:
-
l
- 要移除的MenuKeyListener
-
getMenuKeyListeners
返回添加到此JMenuItem中的所有MenuKeyListener
的数组。- 返回:
-
所有已添加的
MenuKeyListener
或如果未添加任何监听器则返回空数组 - 自:
- 1.4
-
paramString
返回此JMenuItem
的字符串表示形式。此方法仅用于调试目的,返回的字符串的内容和格式可能因实现而异。返回的字符串可能为空但不能为null
。- 覆盖:
-
paramString
在类AbstractButton
- 返回:
-
此
JMenuItem
的字符串表示形式
-
getAccessibleContext
返回与此JMenuItem
关联的AccessibleContext
。对于JMenuItem
,AccessibleContext
采用AccessibleJMenuItem
的形式。如果需要,将创建一个新的AccessibleJMenuItem实例。- 指定者:
-
getAccessibleContext
在接口Accessible
- 覆盖:
-
getAccessibleContext
在类Component
- 返回:
-
作为此
JMenuItem
的AccessibleContext
的AccessibleJMenuItem
-