- java.lang.Object
-
- java.awt.Component
-
- java.awt.Container
-
- javax.swing.JComponent
-
- javax.swing.JMenuBar
-
- All Implemented Interfaces:
-
ImageObserver
,MenuContainer
,Serializable
,Accessible
,MenuElement
@JavaBean(defaultProperty="UI", description="A container for holding and displaying menus.") public class JMenuBar extends JComponent implements Accessible, MenuElement
一个菜单栏的实现。 您可以在菜单栏中添加JMenu
对象来构建菜单。 当用户选择一个JMenu
对象时,显示其关联的JPopupMenu
,允许用户选择其中一个JMenuItems
。有关使用菜单栏的信息和示例,请参阅“Java教程”中的“ How to Use Menus ”一节。
警告: Swing不是线程安全的。 欲了解更多信息,请参阅Swing's Threading Policy 。
警告:此类的序列化对象与将来的Swing版本不兼容。 当前的序列化支持适用于运行相同版本的Swing的应用程序之间的短期存储或RMI。 从1.4开始,支持所有JavaBeans的长期存储已被添加到
java.beans
包中。 请参阅XMLEncoder
。警告:默认情况下,按下Tab键不会从转移焦点
JMenuBar
被添加到与其他Swing组件的容器一起,因为focusTraversalKeysEnabled
财产JMenuBar
设置为false
。 要解决这个问题,你应该调用JMenuBar.setFocusTraversalKeysEnabled(true)
方法。- 从以下版本开始:
- 1.2
- 另请参见:
-
JMenu
,JPopupMenu
,JMenuItem
, Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class 描述 protected class
JMenuBar.AccessibleJMenuBar
这个类实现了JMenuBar
类的可访问性支持。-
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
-
Nested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainer
-
Nested classes/interfaces inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
-
-
Field Summary
-
Fields inherited from class java.awt.Component
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
-
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
-
Fields inherited from class javax.swing.JComponent
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
-
-
构造方法摘要
构造方法 Constructor 描述 JMenuBar()
创建一个新的菜单栏。
-
方法摘要
所有方法 接口方法 具体的方法 弃用的方法 Modifier and Type 方法 描述 JMenu
add(JMenu c)
将指定的菜单附加到菜单栏的末尾。void
addNotify()
覆盖JComponent.addNotify
以使用当前的键盘管理器注册该菜单栏。AccessibleContext
getAccessibleContext()
获取与此JMenuBar关联的AccessibleContext。Component
getComponent()
实施为MenuElement
。Component
getComponentAtIndex(int i)
已过时。替换为getComponent(int i)
int
getComponentIndex(Component c)
返回指定组件的索引。JMenu
getHelpMenu()
获取菜单栏的帮助菜单。Insets
getMargin()
返回菜单边框和菜单之间的边距。JMenu
getMenu(int index)
返回菜单栏中指定位置的菜单。int
getMenuCount()
返回菜单栏中的项目数。SingleSelectionModel
getSelectionModel()
返回处理单个选择的模型对象。MenuElement[]
getSubElements()
实现为MenuElement
- 返回此菜单栏中的菜单。MenuBarUI
getUI()
返回菜单栏当前的UI。String
getUIClassID()
返回呈现此组件的L&F类的名称。boolean
isBorderPainted()
如果菜单栏边框应该绘制,则返回true。boolean
isSelected()
如果菜单栏当前已选择一个组件,则返回true。void
menuSelectionChanged(boolean isIncluded)
实现是一个MenuElement
- 什么也没做。protected void
paintBorder(Graphics g)
如果BorderPainted
属性为true,则绘制菜单栏的边框。protected String
paramString()
返回此JMenuBar
的字符串表示JMenuBar
。protected boolean
processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed)
子类检查所有的子菜单。void
processKeyEvent(KeyEvent e, MenuElement[] path, MenuSelectionManager manager)
实现是一个MenuElement
- 什么也没做。void
processMouseEvent(MouseEvent event, MenuElement[] path, MenuSelectionManager manager)
实现成为一个MenuElement
- 什么也没做。void
removeNotify()
覆盖JComponent.removeNotify
以使用当前的键盘管理器注销此菜单栏。void
setBorderPainted(boolean b)
设置是否应绘制边框。void
setHelpMenu(JMenu menu)
设置用户在菜单栏中选择“帮助”选项时出现的帮助菜单。void
setMargin(Insets m)
设置菜单边界和菜单之间的边距。void
setSelected(Component sel)
设置当前选择的组件,产生对选择模型的更改。void
setSelectionModel(SingleSelectionModel model)
设置模型对象以处理单个选择。void
setUI(MenuBarUI ui)
设置渲染此组件的L&F对象。void
updateUI()
将UI属性重新设置为来自当前外观的值。-
Methods inherited from 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, 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, 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
-
Methods inherited from class 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, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree
-
Methods inherited from class javax.swing.JComponent
addAncestorListener, 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, 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, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
-
-
-
-
方法详细信息
-
getUI
public MenuBarUI getUI()
返回菜单栏当前的UI。- 重写:
-
getUI
在JComponent
- 结果
-
一个
MenuBarUI
是MenuBarUI
目前的L&F对象 - 另请参见:
-
setUI(javax.swing.plaf.MenuBarUI)
-
setUI
@BeanProperty(hidden=true, visualUpdate=true, description="The UI object that implements the Component\'s LookAndFeel.") public void setUI(MenuBarUI ui)
设置渲染此组件的L&F对象。- 参数
-
ui
- 新的MenuBarUI L&F对象 - 另请参见:
-
UIDefaults.getUI(javax.swing.JComponent)
-
updateUI
public void updateUI()
将UI属性重新设置为来自当前外观的值。- 重写:
-
updateUI
在JComponent
- 另请参见:
-
JComponent.updateUI()
-
getUIClassID
@BeanProperty(bound=false) public String getUIClassID()
返回呈现此组件的L&F类的名称。- 重写:
-
getUIClassID
中的JComponent
- 结果
- 字符串“MenuBarUI”
- 另请参见:
-
JComponent.getUIClassID()
,UIDefaults.getUI(javax.swing.JComponent)
-
getSelectionModel
public SingleSelectionModel getSelectionModel()
返回处理单个选择的模型对象。- 结果
-
SingleSelectionModel
属性 - 另请参见:
-
SingleSelectionModel
-
setSelectionModel
@BeanProperty(description="The selection model, recording which child is selected.") public void setSelectionModel(SingleSelectionModel model)
设置模型对象以处理单个选择。- 参数
-
model
- 要使用的SingleSelectionModel
- 另请参见:
-
SingleSelectionModel
-
getMenu
public JMenu getMenu(int index)
返回菜单栏中指定位置的菜单。- 参数
-
index
- 在菜单栏中给出位置的整数,其中0是第一个位置 - 结果
-
该位置的
JMenu
,或null
,如果该位置没有JMenu
(即,如果是JMenuItem
)
-
getMenuCount
@BeanProperty(bound=false) public int getMenuCount()
返回菜单栏中的项目数。- 结果
- 菜单栏中的项目数
-
setHelpMenu
public void setHelpMenu(JMenu menu)
设置用户在菜单栏中选择“帮助”选项时出现的帮助菜单。 此方法尚未实现,并将抛出异常。- 参数
-
menu
- 为用户提供帮助的JMenu
-
getHelpMenu
public JMenu getHelpMenu()
获取菜单栏的帮助菜单。 此方法尚未实现,并将抛出异常。- 结果
-
JMenu
为用户提供帮助
-
getComponentAtIndex
@Deprecated public Component getComponentAtIndex(int i)
已过时。 替换为getComponent(int i)
返回指定索引处的组件。- 参数
-
i
- 一个指定位置的整数,其中0是第一个 - 结果
-
Component
在该位置,或null
为无效索引
-
getComponentIndex
public int getComponentIndex(Component c)
返回指定组件的索引。- 参数
-
c
-Component
- 结果
- 一个给出组件位置的整数,其中0是第一个; 或-1如果不能找到
-
setSelected
public void setSelected(Component sel)
设置当前选择的组件,产生对选择模型的更改。- 参数
-
sel
- 要选择的Component
-
isSelected
@BeanProperty(bound=false) public boolean isSelected()
如果菜单栏当前已选择一个组件,则返回true。- 结果
- 如果已经做出选择,则为true,否则为false
-
isBorderPainted
public boolean isBorderPainted()
如果菜单栏边框应该绘制,则返回true。- 结果
- 如果边框应该涂漆,否则为假
-
setBorderPainted
@BeanProperty(visualUpdate=true, description="Whether the border should be painted.") public void setBorderPainted(boolean b)
设置是否应绘制边框。- 参数
-
b
- 如果true和border属性不是null
,则绘制边框。 - 另请参见:
-
isBorderPainted()
-
paintBorder
protected void paintBorder(Graphics g)
如果BorderPainted
属性为true,则绘制菜单栏的边框。- 重写:
-
paintBorder
在JComponent
- 参数
-
g
- 用于绘画的Graphics
上下文 - 另请参见:
-
JComponent.paint(java.awt.Graphics)
,JComponent.setBorder(javax.swing.border.Border)
-
setMargin
@BeanProperty(visualUpdate=true, description="The space between the menubar\'s border and its contents") public void setMargin(Insets m)
设置菜单边界和菜单之间的边距。 设置为null
将导致菜单栏使用默认边距。- 参数
-
m
- 包含边距值的Insets对象 - 另请参见:
-
Insets
-
getMargin
public Insets getMargin()
返回菜单边框和菜单之间的边距。 如果没有以前的保证金,它将创建一个零大小的默认保证金。- 结果
-
一个包含边距值的
Insets
对象 - 另请参见:
-
Insets
-
processMouseEvent
public void processMouseEvent(MouseEvent event, MenuElement[] path, MenuSelectionManager manager)
实现成为一个MenuElement
- 什么也没做。- Specified by:
-
processMouseEvent
在接口MenuElement
- 参数
-
event
- aMouseEvent
待处理 -
path
- 菜单层次结构中接收元素的路径 -
manager
- 菜单层次结构的MenuSelectionManager
- 另请参见:
-
getSubElements()
-
processKeyEvent
public void processKeyEvent(KeyEvent e, MenuElement[] path, MenuSelectionManager manager)
实现成为一个MenuElement
- 什么也没做。- Specified by:
-
processKeyEvent
在接口MenuElement
- 参数
-
e
- aKeyEvent
待处理 -
path
- 菜单层次结构中接收元素的路径 -
manager
- 菜单层次结构的MenuSelectionManager
- 另请参见:
-
getSubElements()
-
menuSelectionChanged
public void menuSelectionChanged(boolean isIncluded)
实现成为一个MenuElement
- 什么也没做。- Specified by:
-
menuSelectionChanged
在接口MenuElement
- 参数
-
isIncluded
- 可以用于指示此MenuElement
是否处于活动状态(如果是菜单),或是位于更改菜单路径的部分(如果是菜单项)。 - 另请参见:
-
getSubElements()
-
getSubElements
@BeanProperty(bound=false) public MenuElement[] getSubElements()
实现为MenuElement
- 返回此菜单栏中的菜单。 这是实现MenuElement
界面的原因,因此菜单栏可以与其他菜单元素相同。- Specified by:
-
getSubElements
在接口MenuElement
- 结果
- 菜单栏中的一系列菜单项。
-
getComponent
public Component getComponent()
实现成为一个MenuElement
。 返回此对象。- Specified by:
-
getComponent
在接口MenuElement
- 结果
-
当前
Component
(这) - 另请参见:
-
getSubElements()
-
paramString
protected String paramString()
返回此JMenuBar
的字符串表示JMenuBar
。 该方法仅用于调试目的,并且返回的字符串的内容和格式可能因实现而异。 返回的字符串可能为空,但可能不是null
。- 重写:
-
paramString
在JComponent
- 结果
-
这个
JMenuBar
的字符串表示JMenuBar
-
getAccessibleContext
@BeanProperty(bound=false) public AccessibleContext getAccessibleContext()
获取与此JMenuBar关联的AccessibleContext。 对于JMenuBars,AccessibleContext采用AccessibleJMenuBar的形式。 如有必要,将创建一个新的AccessibleJMenuBar实例。- Specified by:
-
getAccessibleContext
在接口Accessible
- 重写:
-
getAccessibleContext
在Component
- 结果
- 一个AccessibleJMenuBar,作为这个JMenuBar的AccessibleContext
-
processKeyBinding
protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed)
子类检查所有的子菜单。- 重写:
-
processKeyBinding
在JComponent
- 参数
-
ks
-KeyStroke
查询 -
e
-KeyEvent
-
condition
- 以下值之一:- JComponent.WHEN_FOCUSED
- JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
- JComponent.WHEN_IN_FOCUSED_WINDOW
-
pressed
- 如果按下键,则为true - 结果
- 如果对操作有约束力,并且该操作已启用,则为true
- 从以下版本开始:
- 1.3
-
addNotify
public void addNotify()
覆盖JComponent.addNotify
以使用当前的键盘管理器注册该菜单栏。
-
removeNotify
public void removeNotify()
覆盖JComponent.removeNotify
以使用当前的键盘管理器注销此菜单栏。
-
-