📄 menu.html
字号:
<td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JPopupMenu.html#addSeparator()">void addSeparator()</a> </td> <td>Adds a separator to the current end of the popup menu. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JPopupMenu.html#insert(java.awt.Component, int)">void insert(Component, int)</a> </td> <td>Inserts a menu item into the menu at the specified position. The first menu item is at position 0, the second at position 1, and so on. The <code>Component</code> argument specifies the menu item to add. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JPopupMenu.html#remove(int)">void remove(int)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/Container.html#removeAll()">void removeAll()</a> </td> <td>Removes the specified item(s) from the menu. If the argument is an integer, then it specifies the position of the menu item to be removed. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JPopupMenu.html#setLightWeightPopupEnabled(boolean)">static void setLightWeightPopupEnabled(boolean)</a> </td> <td>By default, Swing implements a menu's window using a lightweight component. This can cause problems if you use any heavyweight components in your Swing program, as described in <a href="#popup">Bringing Up a Popup Menu</a>. (This is one of several reasons to avoid using heavyweight components.) As a workaround, invoke <code>JPopupMenu.setLightWeightPopupEnabled(false)</code>. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JPopupMenu.html#show(java.awt.Component, int, int)">void show(Component, int, int)</a> </td> <td>Display the popup menu at the specified <i>x,y</i> position (specified in that order by the integer arguments) in the coordinate system of the specified component. </td> </tr></table><p><table border=1><caption><a name="itemapi">Implementing Menu Items</a></caption><tr><th align=left>Constructor or Method</th><th align=left>Purpose</th></tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JMenuItem.html#JMenuItem()">JMenuItem()</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JMenuItem.html#JMenuItem(java.lang.String)">JMenuItem(String)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JMenuItem.html#JMenuItem(javax.swing.Icon)">JMenuItem(Icon)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JMenuItem.html#JMenuItem(java.lang.String, javax.swing.Icon)">JMenuItem(String, Icon)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JMenuItem.html#JMenuItem(java.lang.String, int)">JMenuItem(String, int)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JMenuItem.html#JMenuItem(javax.swing.Action)">JMenuItem(Action)</a> </td> <td>Creates an ordinary menu item. The icon argument, if present, specifies the icon that the menu item should display. Similarly, the string argument specifies the text that the menu item should display. The integer argument specifies the keyboard mnemonic to use. You can specify any of the relevant VK constants defined in the<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/event/KeyEvent.html">KeyEvent</a> class. For example, to specify the A key, use <code>KeyEvent.VK_A</code>. <p> The constructor with the <code>Action</code> parameter, which was introduced in 1.3, sets the menu item's <code>Action</code>, causing the menu item's properties to be initialized from the <code>Action</code>. See<a class="TutorialLink" target="_top" href="../misc/action.html">How to Use Actions</a> for details. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JCheckBoxMenuItem.html#JCheckBoxMenuItem()">JCheckBoxMenuItem()</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JCheckBoxMenuItem.html#JCheckBoxMenuItem(java.lang.String)">JCheckBoxMenuItem(String)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JCheckBoxMenuItem.html#JCheckBoxMenuItem(javax.swing.Icon)">JCheckBoxMenuItem(Icon)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JCheckBoxMenuItem.html#JCheckBoxMenuItem(java.lang.String, javax.swing.Icon)">JCheckBoxMenuItem(String, Icon)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JCheckBoxMenuItem.html#JCheckBoxMenuItem(java.lang.String, boolean)">JCheckBoxMenuItem(String, boolean)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JCheckBoxMenuItem.html#JCheckBoxMenuItem(java.lang.String, javax.swing.Icon, boolean)">JCheckBoxMenuItem(String, Icon, boolean)</a> </td> <td>Creates a menu item that looks and acts like a check box. The string argument, if any, specifies the text that the menu item should display. If you specify <code>true</code> for the boolean argument, then the menu item is initially selected (checked). Otherwise, the menu item is initially unselected. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JRadioButtonMenuItem.html#JRadioButtonMenuItem()">JRadioButtonMenuItem()</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JRadioButtonMenuItem.html#JRadioButtonMenuItem(java.lang.String)">JRadioButtonMenuItem(String)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JRadioButtonMenuItem.html#JRadioButtonMenuItem(javax.swing.Icon)">JRadioButtonMenuItem(Icon)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JRadioButtonMenuItem.html#JRadioButtonMenuItem(java.lang.String, javax.swing.Icon)">JRadioButtonMenuItem(String, Icon)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JRadioButtonMenuItem.html#JRadioButtonMenuItem(java.lang.String, boolean)">JRadioButtonMenuItem(String, boolean)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JRadioButtonMenuItem.html#JRadioButtonMenuItem(javax.swing.Icon, boolean)">JRadioButtonMenuItem(Icon, boolean)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JRadioButtonMenuItem.html#JRadioButtonMenuItem(java.lang.String, javax.swing.Icon, boolean)">JRadioButtonMenuItem(String, Icon, boolean)</a> </td> <td>Creates a menu item that looks and acts like a radio button. The string argument, if any, specifies the text that the menu item should display. If you specify <code>true</code> for the boolean argument, then the menu item is initially selected. Otherwise, the menu item is initially unselected. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JCheckBoxMenuItem.html#setState(boolean)">void setState(boolean)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JCheckBoxMenuItem.html#getState()">boolean getState()</a> <br> <em>(in <code>JCheckBoxMenuItem</code>) </td> <td>Set or get the selection state of a check box menu item. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/AbstractButton.html#setEnabled(boolean)">void setEnabled(boolean)</a> </td> <td>If the argument is true, enable the menu item. Otherwise, disable the menu item. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/AbstractButton.html#setMnemonic(int)">void setMnemonic(int)</a> </td> <td>Set the mnemonic that enables keyboard navigation to the menu or menu item. Use one of the VK constants defined in the <code>KeyEvent</code> class. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JMenuItem.html#setAccelerator(javax.swing.KeyStroke)">void setAccelerator(KeyStroke)</a> </td> <td>Set the accelerator that activates the menu item. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/AbstractButton.html#setActionCommand(java.lang.String)">void setActionCommand(String)</a> </td> <td>Set the name of the action performed by the menu item. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/AbstractButton.html#addActionListener(java.awt.event.ActionListener)">void addActionListener(ActionListener)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/AbstractButton.html#addItemListener(java.awt.event.ItemListener)">void addItemListener(ItemListener)</a> </td> <td>Add an event listener to the menu item. See <a href="#event">Handling Events from Menu Items</a> for details. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/AbstractButton.html#setAction(javax.swing.Action)">void setAction(Action)</a> </td> <td>Set the <code>Action</code> associated with the menu item. See<a class="TutorialLink" target="_top" href="../misc/action.html">How to Use Actions</a> for details. </td> </tr> <tr> <td colspan=2> Many of the preceding methods are inherited from <code>AbstractButton</code>. See <a href="button.html#api">The Button API</a> for information about other useful methods that <code>AbstractButton</code> provides. </td> </tr></table><p></blockquote><h3><a name="eg">Examples that Use Menus</a></h3><blockquote>Menus are used in a few of our examples.<p><table><tr><th align=left> Example</th><th align=left> Where Described</th><th align=left> Notes</th></tr><tr valign=top><td> <a href="examples/index.html#MenuLookDemo"><code>MenuLookDemo</code></a></td><td> This section (<a href="#create">Creating Menus</a>)</td><td> A simple example that creates all kinds of menus except popup menus, but doesn't handle events from the menu items.</td></tr><tr valign=top><td> <a href="examples/index.html#MenuDemo"><code>MenuDemo</code></a></td><td> This section (<a href="#event">Handling Events from Menu Items</a>)</td><td> Adds event handling to <code>MenuLookDemo</code>.</td></tr><tr valign=top><td> <a href="examples/index.html#PopupMenuDemo"><code>PopupMenuDemo</code></a></td><td> This section (<a href="#popup">Bringing Up a Popup Menu</a>)</td><td> Adds popup menus to <code>MenuDemo</code>.</td></tr><tr valign=top><td> <a href="examples/index.html#MenuGlueDemo"><code>MenuGlueDemo</code></a></td><td> This section (<a href="#custom">Customizing Menu Layout</a>)</td><td> Demonstrates affecting menu layout by adding an invisible components to the menu bar.</td></tr><tr valign=top><td> <a href="examples/index.html#MenuLayoutDemo"><code>MenuLayoutDemo</code></a></td><td> This section (<a href="#custom">Customizing Menu Layout</a>)</td><td> Implements sideways-opening menus arranged in a vertical menu bar.</td></tr><tr valign=top><td> <a href="examples/index.html#MenuSelectionManagerDemo"><code>MenuSelectionManagerDemo</code></a></td><td> —</td><td> Adds highlight detection to MenuDemo. To see this feature, click a menu and then move the mouse over any menu item or submenu. Once per second, the text area will be updated with information about the currently highlighted menu item, not to be confused with the menu item that the user eventually chooses. This demo uses the default<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/MenuSelectionManager.html"><code>MenuSelectionManager</code></a>, which tracks the state of the menu hierarchy.</td></tr><tr valign=top><td> <a href="../misc/examples/index.html#ActionDemo"><code>ActionDemo</code></a></td><td> <a href="../misc/action.html">How to Use Actions</a></td><td> Uses <code>Action</code> objects to implement menu items that duplicate functionality provided by tool bar buttons.</td></tr><tr valign=top><td> <a href="examples/index.html#Framework"><code>Framework</code></a></td><td> —</td><td> Brings up multiple identical frames, each with a menu in its menu bar.</td></tr><tr valign=top><td> <a href="examples/index.html#InternalFrameDemo"><code>InternalFrameDemo</code></a></td><td> <a href="internalframe.html">How to Use Internal Frames</a></td><td> Uses a menu item to create windows.</td></tr><tr valign=top><td> <a class="TutorialLink" target="_top" href="../misc/examples/index.html#DragColorTextFieldDemo"><code>DragColorTextFieldDemo</code></a></td><td> <a class="TutorialLink" target="_top" href="../dnd/intro.html">Introduction to Drag and Drop and Data Transfer</a></td><td> Sets up menu items for cut/copy/paste.</td></tr><tr valign=top><td> <a class="TutorialLink" target="_top" href="../misc/examples/index.html#DragPictureDemo"><code>DragPictureDemo</code></a></td><td> <a class="TutorialLink" target="_top" href="../dnd/intro.html">Introduction to Drag and Drop and Data Transfer</a></td><td> Sets up menu items for cut/copy/paste with a non-text component.</td></tr></table> </blockquote> <div class=NavBit> <a target=_top href=list.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=panel.html>Next »</a> </div> </div> <div id=Footer><div id=TagNotes> Problems with the examples? Try <a target="_blank" href=../../information/run-examples.html>Compiling and Running the Examples: FAQs</a>. <br> Complaints? Compliments? Suggestions? <a target="_blank" href="http://developer.sun.com/contact/tutorial_feedback.jsp">Give us your feedback</a>.<br><br> <a target="_blank" href="../../information/copyright.html">Copyright</a> 1995-2006 Sun Microsystems, Inc. All rights reserved. <span id=Download></span></div> </div> <div class=PrintHeaders> <b>Previous page:</b> How to Use Lists <br><b>Next page:</b> How to Use Panels </div> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -