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)
方法。
JMenu
, JPopupMenu
, JMenuItem
Modifier and Type | Class and Description |
---|---|
protected class |
JMenuBar.AccessibleJMenuBar
这个类实现了可访问性支持
JMenuBar 类。
|
JComponent.AccessibleJComponent
Container.AccessibleAWTContainer
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
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_ALIGNMENT
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
Constructor and Description |
---|
JMenuBar()
创建一个新的菜单栏。
|
Modifier and Type | Method and Description |
---|---|
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 属性为真,则
BorderPainted 菜单栏的边框。
|
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属性重新设置为来自当前外观的值。
|
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
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
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, 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, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
public MenuBarUI getUI()
public void setUI(MenuBarUI ui)
ui
- 新的MenuBarUI L&F对象
UIDefaults.getUI(javax.swing.JComponent)
public void updateUI()
updateUI
在
JComponent
JComponent.updateUI()
public String getUIClassID()
getUIClassID
在
JComponent
JComponent.getUIClassID()
,
UIDefaults.getUI(javax.swing.JComponent)
public SingleSelectionModel getSelectionModel()
SingleSelectionModel
属性
SingleSelectionModel
public void setSelectionModel(SingleSelectionModel model)
model
-
SingleSelectionModel
使用
SingleSelectionModel
public JMenu getMenu(int index)
index
- 在菜单栏中给出位置的整数,其中0是第一个位置
JMenu
,或
null
如果在该位置没有
JMenu
(即如果是
JMenuItem
)
public int getMenuCount()
public void setHelpMenu(JMenu menu)
menu
- 为用户提供帮助的JMenu
public JMenu getHelpMenu()
JMenu
@Deprecated public Component getComponentAtIndex(int i)
getComponent(int i)
i
- 一个指定位置的整数,其中0是第一个
Component
在该位置,或
null
为无效索引
public int getComponentIndex(Component c)
c
-
Component
找
public void setSelected(Component sel)
sel
-
Component
选择
public boolean isSelected()
public boolean isBorderPainted()
public void setBorderPainted(boolean b)
b
- 如果true和border属性不是
null
,则绘制边框。
isBorderPainted()
protected void paintBorder(Graphics g)
BorderPainted
属性为true,则
BorderPainted
菜单栏的边框。
paintBorder
在
JComponent
g
- 用于绘画的
Graphics
上下文
JComponent.paint(java.awt.Graphics)
,
JComponent.setBorder(javax.swing.border.Border)
public void setMargin(Insets m)
null
将导致菜单栏使用默认边距。
m
- 包含边距值的Insets对象
Insets
public Insets getMargin()
Insets
边距值的
Insets
对象
Insets
public void processMouseEvent(MouseEvent event, MenuElement[] path, MenuSelectionManager manager)
MenuElement
- 什么也没做。
processMouseEvent
在接口
MenuElement
getSubElements()
public void processKeyEvent(KeyEvent e, MenuElement[] path, MenuSelectionManager manager)
MenuElement
- 什么也没做。
processKeyEvent
在接口
MenuElement
getSubElements()
public void menuSelectionChanged(boolean isIncluded)
MenuElement
- 什么也没做。
menuSelectionChanged
在接口
MenuElement
getSubElements()
public MenuElement[] getSubElements()
MenuElement
- 返回此菜单栏中的菜单。
这是用于实现的原因MenuElement
接口-使得菜单栏可被处理同其他菜单元素。
getSubElements
在接口
MenuElement
public Component getComponent()
MenuElement
。
返回此对象。
getComponent
在界面
MenuElement
Component
(这)
getSubElements()
protected String paramString()
JMenuBar
的字符串表示JMenuBar
。
该方法仅用于调试目的,并且返回的字符串的内容和格式可能因实现而异。
返回的字符串可能为空,但可能不是null
。
paramString
在
JComponent
JMenuBar
的字符串表示
JMenuBar
public AccessibleContext getAccessibleContext()
getAccessibleContext
在界面
Accessible
getAccessibleContext
在
Component
protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed)
processKeyBinding
在
JComponent
ks
-
KeyStroke
查询
e
-
KeyEvent
condition
- 以下值之一:
pressed
- 如果按下键,则为true
public void addNotify()
JComponent.addNotify
以使用当前的键盘管理器注册此菜单栏。
public void removeNotify()
JComponent.removeNotify
以使用当前的键盘管理器取消注册此菜单栏。
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.