站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > JDK 5 Documentation v1.2.2, Java 2 SDK 英文文档

Java(TM) 2 Platform, Standard Edition, v1.2.2 API Specification: Class JMenu - JDK 5 Documentation v1.2.2, Java 2 SDK 英文文档

JavaTM 2 Platform
Standard Edition

javax.swing
Class JMenu

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Container
              |
              +--javax.swing.JComponent
                    |
                    +--javax.swing.AbstractButton
                          |
                          +--javax.swing.JMenuItem
                                |
                                +--javax.swing.JMenu

public class JMenu
extends JMenuItem
implements Accessible, MenuElement

An implementation of a menu -- a popup window containing JMenuItems that is displayed when the user selects an item on the JMenuBar. In addition to JMenuItems, a JMenu can also contain JSeparators.

In essence, a menu is a button with an associated JPopupMenu. When the "button" is pressed, the JPopupMenu appears. If the "button" is on the JMenuBar, the menu is a top-level window. If the "button" is another menu item, then the JPopupMenu is "pull-right" menu.

For the keyboard keys used by this component in the standard Look and Feel (L&F) renditions, see the JMenu key assignments.

Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. A future release of Swing will provide support for long term persistence.

See Also:
JMenuItem, JSeparator, JMenuBar, JPopupMenu, Serialized Form

Inner Class Summary
protected  class JMenu.AccessibleJMenu
          The class used to obtain the accessible role for this object.
protected  class JMenu.WinListener
          A listener class that watches for a popup window closing.
 
Inner classes inherited from class javax.swing.JMenuItem
JMenuItem.AccessibleJMenuItem
 
Inner classes inherited from class javax.swing.AbstractButton
AbstractButton.AccessibleAbstractButton, AbstractButton.ButtonChangeListener
 
Inner classes inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
 
Field Summary
protected  JMenu.WinListener popupListener
          The window-closing listener for the popup.
 
Fields inherited from class javax.swing.AbstractButton
actionListener, 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
 
Fields inherited from class javax.swing.JComponent
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Constructor Summary
JMenu()
          Creates a new JMenu with no text.
JMenu(String s)
          Creates a new JMenu with the supplied string as its text
JMenu(String s, boolean b)
          Creates a new JMenu with the supplied string as its text and specified as a tear-off menu or not.
 
Method Summary
 JMenuItem add(Action a)
          Creates a new menuitem attached to the specified Action object and appends it to the end of this menu.
 Component add(Component c)
          Appends a component to the end of this menu.
 JMenuItem add(JMenuItem menuItem)
          Appends a menuitem to the end of this menu.
 JMenuItem add(String s)
          Creates a new menuitem with the specified text and appends it to the end of this menu.
 void addMenuListener(MenuListener l)
          Add a listener for menu events
 void addSeparator()
          Append a new separator to the end of the menu.
protected  PropertyChangeListener createActionChangeListener(JMenuItem b)
           
protected  JMenu.WinListener createWinListener(JPopupMenu p)
          Create a window-closing listener for the popup.
 void doClick(int pressTime)
          Programatically perform a "click".
protected  void fireMenuCanceled()
          Notify all listeners that have registered interest for notification on this event type.
protected  void fireMenuDeselected()
          Notify all listeners that have registered interest for notification on this event type.
protected  void fireMenuSelected()
          Notify all listeners that have registered interest for notification on this event type.
 AccessibleContext getAccessibleContext()
          Get the AccessibleContext associated with this JComponent
 Component getComponent()
          This method returns the java.awt.Component used to paint this MenuElement.
 int getDelay()
          Returns the suggested delay before the menu's PopupMenu is popped up or down.
 JMenuItem getItem(int pos)
          Returns the JMenuItem at the specified position.
 int getItemCount()
          Returns the number of items on the menu, including separators.
 Component getMenuComponent(int n)
          Returns the component at position n
 int getMenuComponentCount()
          Returns the number of components on the menu.
 Component[] getMenuComponents()
          Returns an array of the menu's subcomponents
 JPopupMenu getPopupMenu()
          Returns the popupmenu associated with this menu
 MenuElement[] getSubElements()
          Returns an array containing the sub-menu components for this menu component
 String getUIClassID()
          Returns the name of the L&F class that renders this component.
 JMenuItem insert(Action a, int pos)
          Insert a new menuitem attached to the specified Action object at a given position.
 JMenuItem insert(JMenuItem mi, int pos)
          Insert the specified JMenuitem at a given position.
 void insert(String s, int pos)
          Insert a new menuitem with the specified text at a given position.
 void insertSeparator(int index)
          Inserts a separator at the specified position.
 boolean isMenuComponent(Component c)
          Returns true if the specified component exists in the submenu hierarchy.
 boolean isPopupMenuVisible()
          Returns true if the menu's popup window is visible.
 boolean isSelected()
          Returns true if the menu is currently selected (popped up).
 boolean isTearOff()
          Returns true if the menu can be torn off.
 boolean isTopLevelMenu()
          Returns true if the menu is a 'top-level menu', that is, if it is the direct child of a menubar.
 void menuSelectionChanged(boolean isIncluded)
          Messaged when the menubar selection changes to activate or deactivate this menu.
protected  String paramString()
          Returns a string representation of this JMenu.
protected  void processKeyEvent(KeyEvent e)
          Override processKeyEvent to process events
 void remove(Component c)
          Removes the Component from this menu.
 void remove(int pos)
          Removes the menu item at the specified index from this menu.
 void remove(JMenuItem item)
          Removes the specified menu item from this menu.
 void removeAll()
          Remove all menu items from this menu.
 void removeMenuListener(MenuListener l)
          Remove a listener for menu events
 void setAccelerator(KeyStroke keyStroke)
          setAccelerator() is not defined for JMenu.
 void setDelay(int d)
          Sets the suggested delay before the menu's PopupMenu is popped up or down.
 void setMenuLocation(int x, int y)
          Set the location of the popup component
 void setModel(ButtonModel newModel)
          Set the data model for the "menu button" -- the label that the user clicks to open or close the menu.
 void setPopupMenuVisible(boolean b)
          Set the visibility of the Menu's popup portion.
 void setSelected(boolean b)
          Sets the selection status of the menu.
 void updateUI()
          Notification from the UIFactory that the L&F has changed.
 
Methods inherited from class javax.swing.JMenuItem
addMenuDragMouseListener, addMenuKeyListener, fireMenuDragMouseDragged, fireMenuDragMouseEntered, fireMenuDragMouseExited, fireMenuDragMouseReleased, fireMenuKeyPressed, fireMenuKeyReleased, fireMenuKeyTyped, getAccelerator, init, isArmed, processKeyEvent, processMenuDragMouseEvent, processMenuKeyEvent, processMouseEvent, removeMenuDragMouseListener, removeMenuKeyListener, setArmed, setEnabled, setUI
 
Methods inherited from class javax.swing.AbstractButton
addActionListener, addChangeListener, addItemListener, checkHorizontalKey, checkVerticalKey, createActionListener, createChangeListener, createItemListener, doClick, fireActionPerformed, fireItemStateChanged, fireStateChanged, getActionCommand, getDisabledIcon, getDisabledSelectedIcon, getHorizontalAlignment, getHorizontalTextPosition, getIcon, getLabel, getMargin, getMnemonic, getModel, getPressedIcon, getRolloverIcon, getRolloverSelectedIcon, getSelectedIcon, getSelectedObjects, getText, getUI, getVerticalAlignment, getVerticalTextPosition, isBorderPainted, isContentAreaFilled, isFocusPainted, isRolloverEnabled, paintBorder, removeActionListener, removeChangeListener, removeItemListener, setActionCommand, setBorderPainted, setContentAreaFilled, setDisabledIcon, setDisabledSelectedIcon, setFocusPainted, setHorizontalAlignment, setHorizontalTextPosition, setIcon, setLabel, setMargin, setMnemonic, setMnemonic, setPressedIcon, setRolloverEnabled, setRolloverIcon, setRolloverSelectedIcon, setSelectedIcon, setText, setUI, setVerticalAlignment, setVerticalTextPosition
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getAlignmentX, getAlignmentY, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getConditionForKeyStroke, getDebugGraphicsOptions, getGraphics, getHeight, getInsets, getInsets, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getVisibleRect, getWidth, getX, getY, grabFocus, hasFocus, isDoubleBuffered, isFocusCycleRoot, isFocusTraversable, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintChildren, paintComponent, paintImmediately, paintImmediately, processComponentKeyEvent, processFocusEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setDebugGraphicsOptions, setDoubleBuffered, setFont, setForeground, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setUI, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, addContainerListener, addImpl, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getLayout, insets, invalidate, isAncestorOf, layout, list, list, locate, minimumSize, paintComponents, preferredSize, print, printComponents, processContainerEvent, processEvent, removeContainerListener, setCursor, setLayout, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, getBackground, getBounds, getColorModel, getComponentOrientation, getCursor, getDropTarget, getFont, getFontMetrics, getForeground, getInputContext, getInputMethodRequests, getLocale, getLocation, getLocationOnScreen, getName, getParent, getPeer, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hide, imageUpdate, inside, isDisplayable, isEnabled, isLightweight, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, printAll, processComponentEvent, processInputMethodEvent, processMouseEvent, remove, removeComponentListener, removeFocusListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setDropTarget, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

popupListener

protected JMenu.WinListener popupListener
The window-closing listener for the popup.
See Also:
JMenu.WinListener
Constructor Detail

JMenu

public JMenu()
Creates a new JMenu with no text.

JMenu

public JMenu(String s)
Creates a new JMenu with the supplied string as its text
Parameters:
s - The text for the menu label

JMenu

public JMenu(String s,
             boolean b)
Creates a new JMenu with the supplied string as its text and specified as a tear-off menu or not.
Parameters:
s - The text for the menu label
b - can the menu be torn off (not yet implemented)
Method Detail

updateUI

public void updateUI()
Notification from the UIFactory that the L&F has changed. Called to replace the UI with the latest version from the UIFactory.
Overrides:
updateUI in class JMenuItem
See Also:
JComponent.updateUI()

getUIClassID

public String getUIClassID()
Returns the name of the L&F class that renders this component.
Overrides:
getUIClassID in class JMenuItem
Returns:
"MenuUI"
See Also:
JComponent.getUIClassID(), UIDefaults.getUI(javax.swing.JComponent)

setModel

public void setModel(ButtonModel newModel)
Set the data model for the "menu button" -- the label that the user clicks to open or close the menu.
Overrides:
setModel in class AbstractButton
Parameters:
m - the ButtonModel
See Also:
AbstractButton.getModel()

isSelected

public boolean isSelected()
Returns true if the menu is currently selected (popped up).
Overrides:
isSelected in class AbstractButton
Returns:
true if the menu is open, else false

setSelected

public void setSelected(boolean b)
Sets the selection status of the menu.
Overrides:
setSelected in class AbstractButton
Parameters:
b - a boolean value -- true to select the menu and open it, false to unselect the menu and close it

isPopupMenuVisible

public boolean isPopupMenuVisible()
Returns true if the menu's popup window is visible.
Returns:
true if the menu is visible, else false

setPopupMenuVisible

public void setPopupMenuVisible(boolean b)
Set the visibility of the Menu's popup portion. The popup may only be made visible if the menu is itself showing on the screen.
Parameters:
b - a boolean value -- true to make the menu visible, false to hide it

getDelay

public int getDelay()
Returns the suggested delay before the menu's PopupMenu is popped up or down. Each look and feel may determine its own policy for observing the delay property. In most cases, the delay is not observed for top level menus or while dragging.
Returns:
an int -- the number of milliseconds to delay

setDelay

public void setDelay(int d)
Sets the suggested delay before the menu's PopupMenu is popped up or down. Each look and feel may determine its own policy for observing the delay property. In most cases, the delay is not observed for top level menus or while dragging.
Parameters:
d - the number of milliseconds to delay
Throws:
IllegalArgumentException - if the value of d is less than 0.

setMenuLocation

public void setMenuLocation(int x,
                            int y)
Set the location of the popup component
Parameters:
x - the x coordinate of the popup's new position
y - the y coordinate of the popup's new position

add

public JMenuItem add(JMenuItem menuItem)
Appends a menuitem to the end of this menu. Returns the menuitem added.
Parameters:
menuItem - the JMenuitem to be added
Returns:
the JMenuItem added

add

public Component add(Component c)
Appends a component to the end of this menu. Returns the component added.
Overrides:
add in class Container
Parameters:
c - the Component to add
Returns:
the Component added

add

public JMenuItem add(String s)
Creates a new menuitem with the specified text and appends it to the end of this menu.
Parameters:
s - the string for the menuitem to be added

add

public JMenuItem add(Action a)
Creates a new menuitem attached to the specified Action object and appends it to the end of this menu.
Parameters:
a - the Action for the menuitem to be added
See Also:
Action

createActionChangeListener

protected PropertyChangeListener createActionChangeListener(JMenuItem b)

addSeparator

public void addSeparator()
Append a new separator to the end of the menu.

insert

public void insert(String s,
                   int pos)
Insert a new menuitem with the specified text at a given position.
Parameters:
s - the text for the menuitem to add
pos - an int giving the position at which to add the new menuitem

insert

public JMenuItem insert(JMenuItem mi,
                        int pos)
Insert the specified JMenuitem at a given position.
Parameters:
mi - the JMenuitem to add
pos - an int giving the position at which to add the new JMenuitem

insert

public JMenuItem insert(Action a,
                        int pos)
Insert a new menuitem attached to the specified Action object at a given position.
Parameters:
a - the Action object for the menuitem to add
pos - an int giving the position at which to add the new menuitem

insertSeparator

public void insertSeparator(int index)
Inserts a separator at the specified position.
Parameters:
index - an int giving the position at which to insert the menu separator
Throws:
IllegalArgumentException - if the value of index is less than 0.

getItem

public JMenuItem getItem(int pos)
Returns the JMenuItem at the specified position. If the specified position contains a separator, this JMenu is returned.
Parameters:
pos - an int giving the position
Throws:
IllegalArgumentException - if the value of index is less than 0.

getItemCount

public int getItemCount()
Returns the number of items on the menu, including separators. This method is included for AWT compatibility.
Returns:
an int equal to the number of items on the menu
See Also:
getMenuComponentCount()

isTearOff

public boolean isTearOff()
Returns true if the menu can be torn off.
Returns:
true if the menu can be torn off, else false

remove

public void remove(JMenuItem item)
Removes the specified menu item from this menu.
Parameters:
item - the JMenuItem to be removed from the menu

remove

public void remove(int pos)
Removes the menu item at the specified index from this menu.
Overrides:
remove in class Container
Parameters:
index - the position of the item to be removed.
Throws:
IllegalArgumentException - if the value of index is less than 0.

remove

public void remove(Component c)
Removes the Component from this menu.
Overrides:
remove in class Container
Parameters:
c - the component to be removed

removeAll

public void removeAll()
Remove all menu items from this menu.
Overrides:
removeAll in class Container
Tags copied from class: Container
See Also:
Container.add(java.awt.Component), Container.remove(int)

getMenuComponentCount

public int getMenuComponentCount()
Returns the number of components on the menu.
Returns:
an int -- the number of components on the menu

getMenuComponent

public Component getMenuComponent(int n)
Returns the component at position n
Parameters:
n - the position of the component to be returned

getMenuComponents

public Component[] getMenuComponents()
Returns an array of the menu's subcomponents
Returns:
an array of Components

isTopLevelMenu

public boolean isTopLevelMenu()
Returns true if the menu is a 'top-level menu', that is, if it is the direct child of a menubar.
Returns:
true if the menu is activated from the menu bar, false if the menu is activated from a menu item on another menu

isMenuComponent

public boolean isMenuComponent(Component c)
Returns true if the specified component exists in the submenu hierarchy.
Parameters:
c - the Component to be tested
Returns:
true if the component exists

getPopupMenu

public JPopupMenu getPopupMenu()
Returns the popupmenu associated with this menu

addMenuListener

public void addMenuListener(MenuListener l)
Add a listener for menu events
Parameters:
l - the listener to be added

removeMenuListener

public void removeMenuListener(MenuListener l)
Remove a listener for menu events
Parameters:
l - the listener to be removed

fireMenuSelected

protected void fireMenuSelected()
Notify all listeners that have registered interest for notification on this event type. The event instance is lazily created using the parameters passed into the fire method.
See Also:
EventListenerList

fireMenuDeselected

protected void fireMenuDeselected()
Notify all listeners that have registered interest for notification on this event type. The event instance is lazily created using the parameters passed into the fire method.
See Also:
EventListenerList

fireMenuCanceled

protected void fireMenuCanceled()
Notify all listeners that have registered interest for notification on this event type. The event instance is lazily created using the parameters passed into the fire method.
See Also:
EventListenerList

createWinListener

protected JMenu.WinListener createWinListener(JPopupMenu p)
Create a window-closing listener for the popup.
Parameters:
p - the JPopupMenu
See Also:
JMenu.WinListener

menuSelectionChanged

public void menuSelectionChanged(boolean isIncluded)
Messaged when the menubar selection changes to activate or deactivate this menu. Overrides JMenuItem.menuSelectionChanged.
Specified by:
menuSelectionChanged in interface MenuElement
Overrides:
menuSelectionChanged in class JMenuItem
Parameters:
isIncluded - true if this menu is active, false if it is not

getSubElements

public MenuElement[] getSubElements()
Returns an array containing the sub-menu components for this menu component
Specified by:
getSubElements in interface MenuElement
Overrides:
getSubElements in class JMenuItem
Returns:
an array of MenuElement objects

getComponent

public Component getComponent()
This method returns the java.awt.Component used to paint this MenuElement. The returned component is used to convert events and detect if an event is inside a menu component.
Specified by:
getComponent in interface MenuElement
Overrides:
getComponent in class JMenuItem
Tags copied from interface: MenuElement
Returns:
the Component value

setAccelerator

public void setAccelerator(KeyStroke keyStroke)
setAccelerator() is not defined for JMenu. Use setMnemonic() instead.
Overrides:
setAccelerator in class JMenuItem
Tags copied from class: JMenuItem
Parameters:
keyStroke - the KeyStroke which will serve as an accelerator

processKeyEvent

protected void processKeyEvent(KeyEvent e)
Description copied from class: JComponent
Override processKeyEvent to process events
Overrides:
processKeyEvent in class JComponent
Tags copied from class: Component
Parameters:
e - the key event.
See Also:
KeyEvent, KeyListener, Component.addKeyListener(java.awt.event.KeyListener), Component.enableEvents(long)

doClick

public void doClick(int pressTime)
Programatically perform a "click". This overrides the method AbstractButton.doClick(int) in order to make the menu pop up.
Overrides:
doClick in class AbstractButton

paramString

protected String paramString()
Returns a string representation of this JMenu. This method is intended to be used only for debugging purposes, and the content and format of the returned string may vary between implementations. The returned string may be empty but may not be null.
Overrides:
paramString in class JMenuItem
Returns:
a string representation of this JMenu.

getAccessibleContext

public AccessibleContext getAccessibleContext()
Get the AccessibleContext associated with this JComponent
Specified by:
getAccessibleContext in interface Accessible
Overrides:
getAccessibleContext in class JMenuItem
Returns:
the AccessibleContext of this JComponent

JavaTM 2 Platform
Standard Edition

Submit a bug or feature
Java, Java 2D, and JDBC are a trademarks or registered trademarks of Sun Microsystems, Inc. in the US and other countries.
Copyright 1993-1999 Sun Microsystems, Inc. 901 San Antonio Road,
Palo Alto, California, 94303, U.S.A. All Rights Reserved.