java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.JMenuBar
- 所有已实现的接口:
-
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
将被显示,允许用户在其中选择一个JMenuItem
。
有关使用菜单栏的信息和示例,请参见如何使用菜单,这是Java教程中的一个部分。
警告: Swing不是线程安全的。更多信息请参见Swing的线程策略。
警告: 该类的序列化对象将不兼容未来的Swing版本。当前的序列化支持适用于短期存储或在运行相同Swing版本的应用程序之间的RMI。从1.4开始,已将所有JavaBeans的长期存储支持添加到java.beans
包中。请参见XMLEncoder
。
警告: 默认情况下,按Tab键不会从与其他Swing组件一起添加到容器中的JMenuBar
转移焦点,因为JMenuBar
的focusTraversalKeysEnabled
属性设置为false
。要解决此问题,您应该调用JMenuBar.setFocusTraversalKeysEnabled(true)
方法。
- 自版本:
- 1.2
- 参见:
-
Nested Class Summary
Modifier and TypeClassDescriptionprotected class
该类实现了JMenuBar
类的辅助功能支持。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.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
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescription将指定的菜单追加到菜单栏的末尾。void
重写JComponent.addNotify
以向当前键盘管理器注册此菜单栏。获取与此JMenuBar关联的AccessibleContext。实现为MenuElement
。getComponentAtIndex
(int i) 已弃用。int
返回指定组件的索引。获取菜单栏的帮助菜单。返回菜单栏边框与其菜单之间的边距。getMenu
(int index) 返回菜单栏中指定位置的菜单。int
返回菜单栏中的项目数。返回处理单个选择的模型对象。实现为MenuElement
-- 返回此菜单栏中的菜单。getUI()
返回菜单栏的当前UI。返回渲染此组件的L&F类的名称。boolean
如果应绘制菜单栏的边框,则返回true。boolean
如果菜单栏当前有组件被选中,则返回true。void
menuSelectionChanged
(boolean isIncluded) 实现为MenuElement
-- 什么也不做。protected void
如果BorderPainted
属性为true,则绘制菜单栏的边框。protected String
返回此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
重写JComponent.removeNotify
以取消向当前键盘管理器注册此菜单栏。void
setBorderPainted
(boolean b) 设置是否应绘制边框。void
setHelpMenu
(JMenu menu) 设置用户在菜单栏中选择“帮助”选项时显示的帮助菜单。void
设置菜单栏边框与其菜单之间的边距。void
setSelected
(Component sel) 设置当前选定的组件,从而对选择模型进行更改。void
设置处理单个选择的模型对象。void
设置渲染此组件的L&F对象。void
updateUI()
使用当前外观和感觉的值重置UI属性。Methods declared in class javax.swing.JComponent
addAncestorListener, 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, 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
Methods declared in 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 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, 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
-
Constructor Details
-
JMenuBar
public JMenuBar()创建一个新的菜单栏。
-
-
Method Details
-
getUI
返回菜单栏的当前UI。- 重写:
-
getUI
在类JComponent
中 - 返回:
-
一个
MenuBarUI
,即菜单栏的当前L&F对象 - 参见:
-
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对象 - 参见:
-
updateUI
public void updateUI()使用当前外观和感觉的值重置UI属性。- 重写:
-
updateUI
在类JComponent
中 - 参见:
-
getUIClassID
返回渲染此组件的L&F类的名称。- 重写:
-
getUIClassID
在类JComponent
中 - 返回:
- 字符串"MenuBarUI"
- 参见:
-
getSelectionModel
返回处理单个选择的模型对象。- 返回:
-
SingleSelectionModel
属性 - 参见:
-
setSelectionModel
@BeanProperty(description="The selection model, recording which child is selected.") public void setSelectionModel(SingleSelectionModel model) 设置处理单个选择的模型对象。- 参数:
-
model
- 要使用的SingleSelectionModel
- 参见:
-
add
将指定的菜单追加到菜单栏的末尾。- 参数:
-
c
- 要添加的JMenu
组件 - 返回:
- 菜单组件
-
getMenu
返回菜单栏中指定位置的菜单。- 参数:
-
index
- 一个整数,表示菜单栏中的位置,其中0是第一个位置 - 返回:
-
该位置处的
JMenu
,如果该位置没有JMenu
(即如果是JMenuItem
),则返回null
-
getMenuCount
返回菜单栏中的项目数。- 返回:
- 菜单栏中的项目数
-
setHelpMenu
设置用户在菜单栏中选择“帮助”选项时显示的帮助菜单。此方法尚未实现,将引发异常。- 参数:
-
menu
- 为用户提供帮助的JMenu
-
getHelpMenu
获取菜单栏的帮助菜单。此方法尚未实现,将引发异常。- 返回:
-
为用户提供帮助的
JMenu
-
getComponentAtIndex
Deprecated.replaced bygetComponent(int i)
返回指定索引处的组件。- 参数:
-
i
- 指定位置的整数,其中0是第一个 - 返回:
-
该位置处的
Component
,对于无效索引则返回null
-
getComponentIndex
返回指定组件的索引。- 参数:
-
c
- 要查找的Component
- 返回:
- 组件的位置,其中0是第一个;如果找不到则返回-1
-
setSelected
设置当前选定的组件,从而对选择模型进行更改。- 参数:
-
sel
- 要选择的Component
-
isSelected
如果菜单栏当前有组件被选中,则返回true。- 返回:
- 如果已进行选择,则返回true,否则返回false
-
isBorderPainted
public boolean isBorderPainted()如果应绘制菜单栏的边框,则返回true。- 返回:
- 如果应绘制边框,则返回true,否则返回false
-
setBorderPainted
@BeanProperty(visualUpdate=true, description="Whether the border should be painted.") public void setBorderPainted(boolean b) 设置是否应绘制边框。- 参数:
-
b
- 如果为true且边框属性不为null
,则绘制边框。 - 参见:
-
paintBorder
如果BorderPainted
属性为true,则绘制菜单栏的边框。- 覆盖:
-
paintBorder
在类JComponent
中 - 参数:
-
g
- 用于绘制的Graphics
上下文 - 参见:
-
setMargin
@BeanProperty(visualUpdate=true, description="The space between the menubar\'s border and its contents") public void setMargin(Insets m) 设置菜单栏边框与其菜单之间的边距。设置为null
将导致菜单栏使用默认边距。- 参数:
-
m
- 包含边距值的Insets对象 - 参见:
-
getMargin
返回菜单栏边框与其菜单之间的边距。如果没有先前的边距,则将创建一个具有零大小的默认边距。- 返回:
-
包含边距值的
Insets
对象 - 参见:
-
processMouseEvent
实现为一个MenuElement
-- 什么也不做。- 指定者:
-
processMouseEvent
在接口MenuElement
中 - 参数:
-
event
- 要处理的MouseEvent
-
path
- 菜单层次结构中接收元素的路径 -
manager
- 菜单层次结构的MenuSelectionManager
- 参见:
-
processKeyEvent
实现为一个MenuElement
-- 什么也不做。- 指定者:
-
processKeyEvent
在接口MenuElement
中 - 参数:
-
e
- 要处理的KeyEvent
-
path
- 菜单层次结构中接收元素的路径 -
manager
- 菜单层次结构的MenuSelectionManager
- 参见:
-
getSubElements
实现为一个MenuElement
-- 返回此菜单栏中的菜单。这是实现MenuElement
接口的原因 -- 以便菜单栏可以像其他菜单元素一样处理。- 指定者:
-
getSubElements
在接口MenuElement
中 - 返回:
- 菜单栏中菜单项的数组。
-
getComponent
实现为一个MenuElement
。返回此对象。- 指定者:
-
getComponent
在接口MenuElement
中 - 返回:
-
当前的
Component
(此对象) - 参见:
-
paramString
返回此JMenuBar
的字符串表示形式。此方法仅用于调试目的,返回的字符串的内容和格式可能因实现而异。返回的字符串可能为空,但不能为null
。- 覆盖:
-
paramString
在类JComponent
中 - 返回:
-
此
JMenuBar
的字符串表示形式
-
getAccessibleContext
获取与此JMenuBar关联的AccessibleContext。对于JMenuBars,AccessibleContext采用AccessibleJMenuBar的形式。如有必要,将创建一个新的AccessibleJMenuBar实例。- 指定者:
-
getAccessibleContext
在接口Accessible
中 - 覆盖:
-
getAccessibleContext
在类Component
中 - 返回:
- 作为此JMenuBar的AccessibleContext的AccessibleJMenuBar
-
processKeyBinding
子类化以检查所有子菜单。- 覆盖:
-
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起:
- 1.3
-
addNotify
public void addNotify()覆盖JComponent.addNotify
以向当前键盘管理器注册此菜单栏。- 覆盖:
-
addNotify
在类JComponent
中 - 参见:
-
removeNotify
public void removeNotify()覆盖JComponent.removeNotify
以取消向当前键盘管理器注册此菜单栏。- 覆盖:
-
removeNotify
在类JComponent
中 - 参见:
-
getComponent(int i)
替代