- 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 JMenuBarextends JComponentimplements Accessible, MenuElement
菜单栏的实现。 您将JMenu
对象添加到菜单栏以构建菜单。 当用户选择JMenu
对象时,将显示其关联的JPopupMenu
,允许用户在其上选择其中一个JMenuItems
。有关使用菜单栏的信息和示例,请参阅“Java教程”中的 How to Use Menus部分。
警告: Swing不是线程安全的。 有关更多信息,请参阅Swing's Threading Policy 。
警告:此类的序列化对象与以后的Swing版本不兼容。 当前的序列化支持适用于运行相同版本Swing的应用程序之间的短期存储或RMI。 从1.4开始,
java.beans
软件包中添加了对所有JavaBeansjava.beans
长期存储的支持。 请参阅XMLEncoder
。警告:默认情况下,按下Tab键不会从转移焦点
JMenuBar
这是添加到与其他Swing组件的容器一起,因为focusTraversalKeysEnabled
财产JMenuBar
设置为false
。 要解决此问题,您应该调用JMenuBar.setFocusTraversalKeysEnabled(true)
方法。- 从以下版本开始:
- 1.2
- 另请参见:
-
JMenu
,JPopupMenu
,JMenuItem
, Serialized Form
-
-
嵌套类汇总
嵌套类 变量和类型 类 描述 protected class
JMenuBar.AccessibleJMenuBar
此类实现JMenuBar
类的可访问性支持。-
嵌套类/接口声明在类 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
-
Fields declared in interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
-
-
构造方法摘要
构造方法 构造器 描述 JMenuBar()
创建一个新的菜单栏。
-
方法摘要
所有方法 实例方法 具体的方法 弃用的方法 变量和类型 方法 描述 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)
Subclassed检查所有儿童菜单。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属性。-
声明方法的类 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, 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
-
声明方法的类 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
-
声明方法的类 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, hide, 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
-
-
-
-
方法详细信息
-
getUI
public MenuBarUI getUI()
返回菜单栏的当前UI。- 重写:
-
getUI
在类JComponent
- 结果
- a
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
@Deprecatedpublic Component getComponentAtIndex(int i)
Deprecated.replaced bygetComponent(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。- 结果
- 如果边框应该被绘制,则为true,否则为false
-
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
- 待处理的MouseEvent
-
path
- 菜单层次结构中接收元素的路径 -
manager
- 菜单层次结构的MenuSelectionManager
- 另请参见:
-
getSubElements()
-
processKeyEvent
public void processKeyEvent(KeyEvent e, MenuElement[] path, MenuSelectionManager manager)
实施为MenuElement
- 什么都不做。- Specified by:
-
processKeyEvent
在界面MenuElement
- 参数
-
e
- 待处理的KeyEvent
-
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)
Subclassed检查所有儿童菜单。- 重写:
-
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
以使用当前键盘管理器取消注册此菜单栏。
-
-