public class JMenuItem extends AbstractButton implements Accessible, MenuElement
JMenuItem包含在一个JPopupMenu执行完全该功能。
菜单项可以配置,并在一定程度上由Action s控制。
使用带有菜单项的Action除了直接配置菜单项之外还有很多好处。 请参阅Swing Components Supporting Action了解更多详情,您可以找到更多信息How to Use Actions ,在Java教程的部分。
有关进一步的文档和示例,请参阅Java教程中的 How to Use Menus 。
警告: Swing不是线程安全的。 有关更多信息,请参阅Swing's Threading Policy 。
警告:此类的序列化对象与将来的Swing版本不兼容。 当前的序列化支持适用于运行相同版本的Swing的应用程序之间的短期存储或RMI。
从1.4开始,对所有JavaBeans的长期存储的支持已被添加到java.beans包中。 请参阅XMLEncoder 。
JPopupMenu , JMenu , JCheckBoxMenuItem , JRadioButtonMenuItem
| Modifier and Type | Class and Description |
|---|---|
protected class |
JMenuItem.AccessibleJMenuItem
这个类实现了可访问性支持
JMenuItem类。
|
AbstractButton.AccessibleAbstractButton,
AbstractButton.ButtonChangeListenerJComponent.AccessibleJComponentContainer.AccessibleAWTContainer
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategyactionListener,
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
listenerList, TOOL_TIP_TEXT_KEY,
ui, UNDEFINED_CONDITION,
WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENTBOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WESTABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH| Constructor and Description |
|---|
JMenuItem()
创建一个没有设置文本或图标的
JMenuItem 。
|
JMenuItem(Action a)
创建一个菜单项,其属性取自指定的
Action 。
|
JMenuItem(Icon icon)
用指定的图标创建一个
JMenuItem 。
|
JMenuItem(String text)
用指定的文本创建一个
JMenuItem 。
|
JMenuItem(String text,
Icon icon)
用指定的文本和图标创建一个
JMenuItem 。
|
JMenuItem(String text,
int mnemonic)
创建一个
JMenuItem与指定的文本和键盘助记符。
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
actionPropertyChanged(Action action,
String propertyName)
响应关联操作中的属性更改更新按钮的状态。
|
void |
addMenuDragMouseListener(MenuDragMouseListener l)
在菜单项中添加一个
MenuDragMouseListener 。
|
void |
addMenuKeyListener(MenuKeyListener l)
在菜单项中添加一个
MenuKeyListener 。
|
protected void |
configurePropertiesFromAction(Action a)
设置有关该按钮的属性,以匹配指定的
Action 。
|
protected void |
fireMenuDragMouseDragged(MenuDragMouseEvent event)
通知所有在此事件类型上通知有兴趣的听众。
|
protected void |
fireMenuDragMouseEntered(MenuDragMouseEvent event)
通知所有在此事件类型上通知有兴趣的听众。
|
protected void |
fireMenuDragMouseExited(MenuDragMouseEvent event)
通知所有在此事件类型上通知有兴趣的听众。
|
protected void |
fireMenuDragMouseReleased(MenuDragMouseEvent event)
通知所有在此事件类型上通知有兴趣的听众。
|
protected void |
fireMenuKeyPressed(MenuKeyEvent event)
通知所有在此事件类型上通知有兴趣的听众。
|
protected void |
fireMenuKeyReleased(MenuKeyEvent event)
通知所有在此事件类型上通知有兴趣的听众。
|
protected void |
fireMenuKeyTyped(MenuKeyEvent event)
通知所有在此事件类型上通知有兴趣的听众。
|
KeyStroke |
getAccelerator()
返回
KeyStroke作为菜单项的加速器。
|
AccessibleContext
|
getAccessibleContext()
返回
AccessibleContext与此相关
JMenuItem 。
|
Component |
getComponent()
返回用于绘制此对象的
java.awt.Component 。
|
MenuDragMouseListener[]
|
getMenuDragMouseListeners()
返回使用addMenuDragMouseListener()添加到此JMenuItem的所有
MenuDragMouseListener的数组。
|
MenuKeyListener[]
|
getMenuKeyListeners()
返回使用addMenuKeyListener()添加到此JMenuItem的所有
MenuKeyListener的数组。
|
MenuElement[]
|
getSubElements()
此方法返回一个包含此菜单组件的子菜单组件的数组。
|
String |
getUIClassID()
返回用于构造用于呈现此组件的L&F类的名称的后缀。
|
protected void |
init(String text,
Icon icon)
用指定的文本和图标初始化菜单项。
|
boolean |
isArmed()
返回菜单项是否为“布防”。
|
void |
menuSelectionChanged(boolean isIncluded)
由被叫
MenuSelectionManager当
MenuElement被选择或取消选择。
|
protected String
|
paramString()
返回此
JMenuItem的字符串表示
JMenuItem 。
|
void |
processKeyEvent(KeyEvent e,
MenuElement[] path,
MenuSelectionManager manager)
处理来自转发的键事件
MenuSelectionManager和更改菜单选择,如果需要,通过使用
MenuSelectionManager的API。
|
void |
processMenuDragMouseEvent(MenuDragMouseEvent e)
在菜单中处理鼠标拖动。
|
void |
processMenuKeyEvent(MenuKeyEvent e)
在菜单中处理按键。
|
void |
processMouseEvent(MouseEvent e,
MenuElement[] path,
MenuSelectionManager manager)
处理从MenuSelectionManager转发的鼠标事件,并根据
MenuSelectionManager更改菜单选项,方法是使用
MenuSelectionManager的API。
|
void |
removeMenuDragMouseListener(MenuDragMouseListener l)
从菜单项中删除
MenuDragMouseListener 。
|
void |
removeMenuKeyListener(MenuKeyListener l)
从菜单项中删除
MenuKeyListener 。
|
void |
setAccelerator(KeyStroke keyStroke)
设置在不导航菜单层次结构的情况下调用菜单项的动作侦听器的组合键。
|
void |
setArmed(boolean b)
将菜单项标识为“布防”。
|
void |
setEnabled(boolean b)
启用或禁用菜单项。
|
void |
setModel(ButtonModel newModel)
设置此按钮所代表的模型。
|
void |
setUI(MenuItemUI ui)
设置呈现此组件的外观和对象。
|
void |
updateUI()
将UI属性重新设置为来自当前外观的值。
|
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
addAncestorListener,
addNotify, addVetoableChangeListener,
computeVisibleRect,
contains, createToolTip,
disable, enable, firePropertyChange,
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
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
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,
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, getPeer, 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, resize,
resize, setBounds,
setBounds,
setComponentOrientation,
setCursor, setDropTarget,
setFocusable, setFocusTraversalKeysEnabled,
setIgnoreRepaint,
setLocale,
setLocation, setLocation,
setName, setSize,
setSize, show, show, size, toString, transferFocus, transferFocusBackward,
transferFocusUpCycle
public JMenuItem()
JMenuItem 。
public JMenuItem(Icon icon)
JMenuItem 。
icon -的图标
JMenuItem
public JMenuItem(String text)
JMenuItem 。
text - 文字
JMenuItem
public JMenuItem(Action a)
Action 。
a -的的作用
JMenuItem
public JMenuItem(String text, Icon icon)
JMenuItem 。
text - 文字
JMenuItem
icon -的图标
JMenuItem
public JMenuItem(String text, int mnemonic)
JMenuItem 。
text - 文字
JMenuItem
mnemonic - 键盘助记符为
JMenuItem
public void setModel(ButtonModel newModel)
setModel在
AbstractButton
newModel - 全新
ButtonModel
AbstractButton.getModel()
protected void init(String text, Icon icon)
init在
AbstractButton
text - 文字的
JMenuItem
icon -的图标
JMenuItem
public void setUI(MenuItemUI ui)
ui -
JMenuItemUI L&F对象
UIDefaults.getUI(javax.swing.JComponent)
public void updateUI()
updateUI在
AbstractButton
JComponent.updateUI()
public String getUIClassID()
getUIClassID在
JComponent
JComponent.getUIClassID()
,
UIDefaults.getUI(javax.swing.JComponent)
public void setArmed(boolean b)
b - 真的
b菜单项,以便它可以选择
public boolean isArmed()
setArmed(boolean)
public void setEnabled(boolean b)
setEnabled在
AbstractButton
b - true启用项目
Component.isEnabled()
,
Component.isLightweight()
public void setAccelerator(KeyStroke keyStroke)
keyStroke -在
KeyStroke将作为加速器
public KeyStroke getAccelerator()
KeyStroke其作为菜单项的加速器。
KeyStroke加速键的
KeyStroke对象
protected void configurePropertiesFromAction(Action a)
Action 。
请参考Swing Components Supporting Action有关这些属性的更多细节。
configurePropertiesFromAction在
AbstractButton
a -
Action从哪个获取属性,或
null
Action , AbstractButton.setAction(javax.swing.Action)
protected void actionPropertyChanged(Action action, String propertyName)
PropertyChangeListener从返回createActionPropertyChangeListener 。
子类通常不需要调用它。
支持额外Action Action子类应该覆盖此和configurePropertiesFromAction 。
有关此方法设置的属性的列表,请参阅Swing Components Supporting Action的表。
actionPropertyChanged在
AbstractButton
action - 与此按钮相关
Action Action
propertyName - 更改的属性的名称
Action , AbstractButton.configurePropertiesFromAction(javax.swing.Action)
public void processMouseEvent(MouseEvent e, MenuElement[] path, MenuSelectionManager manager)
MenuSelectionManager更改菜单选项,方法是使用MenuSelectionManager的API。
注意:您不必将事件转发到子组件。 这是由MenuSelectionManager 。
processMouseEvent在接口
MenuElement
e - a
MouseEvent
path -
MenuElement路径数组
manager -
MenuSelectionManager
public void processKeyEvent(KeyEvent e, MenuElement[] path, MenuSelectionManager manager)
MenuSelectionManager更改菜单选项,方法是使用MenuSelectionManager的API。
注意:您不必将事件转发到子组件。 这是由MenuSelectionManager 。
processKeyEvent在界面
MenuElement
e - a
KeyEvent
path -
MenuElement路径数组
manager -
MenuSelectionManager
public void processMenuDragMouseEvent(MenuDragMouseEvent e)
e - 一个
MenuDragMouseEvent对象
public void processMenuKeyEvent(MenuKeyEvent e)
e - 一个
MenuKeyEvent对象
protected void fireMenuDragMouseEntered(MenuDragMouseEvent event)
event - a
MenuMouseDragEvent
EventListenerList
protected void fireMenuDragMouseExited(MenuDragMouseEvent event)
event - a
MenuDragMouseEvent
EventListenerList
protected void fireMenuDragMouseDragged(MenuDragMouseEvent event)
event - a
MenuDragMouseEvent
EventListenerList
protected void fireMenuDragMouseReleased(MenuDragMouseEvent event)
event - a
MenuDragMouseEvent
EventListenerList
protected void fireMenuKeyPressed(MenuKeyEvent event)
event - a
MenuKeyEvent
EventListenerList
protected void fireMenuKeyReleased(MenuKeyEvent event)
event - a
MenuKeyEvent
EventListenerList
protected void fireMenuKeyTyped(MenuKeyEvent event)
event - a
MenuKeyEvent
EventListenerList
public void menuSelectionChanged(boolean isIncluded)
MenuSelectionManager当
MenuElement被选中或未选择时调用。
menuSelectionChanged中的
MenuElement
isIncluded -
如果此菜单项位于更改的菜单路径部分,则为true,如果此菜单是更改的菜单路径的一部分,则为false,但该路径的该特定部分仍然相同
MenuSelectionManager.setSelectedPath(MenuElement[])
public MenuElement[] getSubElements()
getSubElements在界面
MenuElement
MenuElement的阵列
public Component getComponent()
java.awt.Component 。
返回的组件将用于转换事件并检测事件是否在菜单组件内。
getComponent中的
MenuElement
Component
public void addMenuDragMouseListener(MenuDragMouseListener l)
MenuDragMouseListener 。
l - 要加入的
MenuDragMouseListener
public void removeMenuDragMouseListener(MenuDragMouseListener l)
MenuDragMouseListener 。
l - 要删除的
MenuDragMouseListener
public MenuDragMouseListener[] getMenuDragMouseListeners()
MenuDragMouseListener的数组。
MenuDragMouseListener如果没有添加侦听器,则为空数组
public void addMenuKeyListener(MenuKeyListener l)
MenuKeyListener 。
l - 要加入的
MenuKeyListener
public void removeMenuKeyListener(MenuKeyListener l)
MenuKeyListener 。
l - 要删除的
MenuKeyListener
public MenuKeyListener[] getMenuKeyListeners()
MenuKeyListener的数组。
MenuKeyListener或者如果没有添加任何监听器,则为空数组
protected String paramString()
JMenuItem的字符串表示JMenuItem 。
该方法仅用于调试目的,并且返回的字符串的内容和格式可能因实现而异。
返回的字符串可能为空,但可能不是null 。
paramString在
AbstractButton
JMenuItem的字符串表示
JMenuItem
public AccessibleContext getAccessibleContext()
AccessibleContext与此相关JMenuItem 。
为JMenuItem s时, AccessibleContext需要一个的形式AccessibleJMenuItem 。
如有必要,将创建一个新的AccessibleJMenuItme实例。
getAccessibleContext中的
Accessible
getAccessibleContext在
Component
AccessibleJMenuItem ,作为这个
AccessibleContext的
JMenuItem