📄 menu.html
字号:
<a name="popup">Bringing Up a Popup Menu</a></h3><blockquote>To bring up a popup menu(<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JPopupMenu.html"><code>JPopupMenu</code></a>), you must register a mouse listeneron each componentthat the popup menu should be associated with.The mouse listener must detectuser requests that the popup menu be brought up.<p>The exact gesture that should bring up a popup menu variesby look and feel.In Microsoft Windows,the user by convention brings up a popup menuby releasing the right mouse buttonwhile the cursor is over a componentthat is popup-enabled.In the Java look and feel, the customary triggeris either pressing the right mouse button (for a popup that goes away when the button is released)or clicking it(for a popup that stays up).In a future release,a new mechanism for automatically triggering popup menusin the appropriate way for the look and feelmight be added; see<a class="OutsideLink" target="_blank" href="http://developer.java.sun.com/developer/bugParade/bugs/4634626.html">bug #4634626</a>.<p>The mouse listenerbrings up the popup menuby invoking the <code>show</code> methodon the appropriate <code>JPopupMenu</code> instance.The following code,taken from<a class="SourceLink" target="_blank" href="examples/PopupMenuDemo.java"><code>PopupMenuDemo.java</code></a>,shows how to create and show popup menus.You can use Java Web Start to<a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/components/examples/PopupMenuDemo.jnlp"><b><i>run PopupMenuDemo</i></b></a> (it requires release 6).<blockquote><pre><em>//...where instance variables are declared:</em>JPopupMenu popup; <em>//...where the GUI is constructed:</em> //Create the popup menu. popup = new JPopupMenu(); menuItem = new JMenuItem("A popup menu item"); menuItem.addActionListener(this); popup.add(menuItem); menuItem = new JMenuItem("Another popup menu item"); menuItem.addActionListener(this); popup.add(menuItem); //Add listener to components that can bring up popup menus. MouseListener popupListener = new PopupListener(); output.addMouseListener(popupListener); menuBar.addMouseListener(popupListener);...class PopupListener extends MouseAdapter { public void mousePressed(MouseEvent e) { maybeShowPopup(e); } public void mouseReleased(MouseEvent e) { maybeShowPopup(e); } private void maybeShowPopup(MouseEvent e) { if (e.isPopupTrigger()) { popup.show(e.getComponent(), e.getX(), e.getY()); } }}</pre></blockquote><p>Popup menus have a fewinteresting implementation details.One is thatevery menu has an associated popup menu.When the menuis activated,it uses its associated popup menuto show its menu items.<p>Another detail is that a popup menu itselfuses another componentto implement the window containing the menu items.Depending on the circumstances under whichthe popup menu is displayed,the popup menu might implement its "window" usinga lightweight component (such as a <code>JPanel</code>),a "mediumweight" component (such as a<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/Panel.html"><code>Panel</code></a>),or a heavyweight window (something that inherits from<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/Window.html"><code>Window</code></a>).<p>Lightweight popup windows are more efficient thanheavyweight windows, but they don't work wellif you have any heavyweight componentsinside your GUI.Specifically, when the lightweight popup's display areaintersects the heavyweight component's display area,the heavyweight component is drawn on top.This is one of the reasons we recommend againstmixing heavyweight and lightweight components.If you absolutely need to use a heavyweight component in your GUI,then you can invoke<code>JPopupMenu.setLightWeightPopupEnabled(false)</code>to disable lightweight popup windows.For details, see<a href="http://java.sun.com/products/jfc/tsc/articles/mixing/index.html">MixingHeavy and Light Components</a>, an article in<em><a href="http://java.sun.com/products/jfc/tsc/index.html">The Swing Connection</a>.</em></blockquote><h3><a name="custom">Customizing Menu Layout</a></h3><blockquote>Because menus are made up of ordinary Swing components,you can easily customize them.For example,you can add any lightweight componentto a <code>JMenu</code> or <code>JMenuBar</code>.And because <code>JMenuBar</code> uses<a href="../layout/box.html"><code>BoxLayout</code></a>,you can customize a menu bar's layoutjust by adding invisible components to it.Here is an exampleof adding a <a href="../layout/box.html#filler">glue</a> component to a menu bar,so thatthe last menu is at the right edge of the menu bar:<p><blockquote><pre><em>//...create and add some menus...</em>menuBar.add(Box.createHorizontalGlue());<em>//...create the rightmost menu...</em>menuBar.add(rightMenu);</pre></blockquote>The following picture shows the result,which you can duplicate by compiling and running<a class="SourceLink" target="_blank" href="examples/MenuGlueDemo.java"><code>MenuGlueDemo.java</code></a> or by using Java Web Start to<a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/components/examples/MenuGlueDemo.jnlp"><b><i>run MenuGlueDemo</i></b></a> (it requires release 6).<p><center><IMG SRC="../../figures/uiswing/components/MenuGlueDemo.png" WIDTH="334" HEIGHT="65" ALIGN="BOTTOM" ALT="MenuGlueDemo"></center></p><p>Another way of changing the look of menusis to change the layout managers used to control them.For example, you can change a menu bar's layout managerfrom the default left-to-right <code>BoxLayout</code>to something such as <code>GridLayout</code>.You can also change how an activated menu or other popup menulays out its items, as<a class="SourceLink" target="_blank" href="examples/MenuLayoutDemo.java"><code>MenuLayoutDemo.java</code></a>demonstrates.You can use Java Web Start to<a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/components/examples/MenuLayoutDemo.jnlp"><b><i>run MenuLayoutDemo</i></b></a> (it requires release 6).Here's a picture of the menu layout that <code>MenuLayoutDemo</code> creates:<p><center><IMG SRC="../../figures/uiswing/components/MenuLayoutDemo.png" WIDTH="777" HEIGHT="150" ALIGN="BOTTOM" ALT="MenuLayoutDemo"></center></p><p></blockquote><h3><a name="api">The Menu API</a></h3><blockquote>The following tables list the commonly usedmenu constructors and methods.The API for using menus falls into these categories:<ul><li><a href="#menubarapi">Creating and Setting Up Menu Bars</a><li><a href="#menuapi">Creating and Populating Menus</a><li><a href="#popupapi">Creating, Populating, and Controlling Popup Menus</a><li><a href="#itemapi">Implementing Menu Items</a></ul><p><table border=1><caption><a name="menubarapi">Creating and Setting Up Menu Bars</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/JMenuBar.html#JMenuBar()">JMenuBar()</a> </td> <td>Creates a menu bar. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JMenuBar.html#add(javax.swing.JMenu)">JMenu add(JMenu)</a> </td> <td>Creates a menu bar. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JFrame.html#setJMenuBar(javax.swing.JMenuBar)">void setJMenuBar(JMenuBar)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JFrame.html#getJMenuBar()">JMenuBar getJMenuBar()</a> <br> <em>(in <code>JApplet</code>, <code>JDialog</code>, <code>JFrame</code>, <code>JInternalFrame</code>, <code>JRootPane</code>)</em> </td> <td>Sets or gets the menu bar of an <a href="applet.html">applet</a>, <a href="dialog.html">dialog</a>, <a href="frame.html">frame</a>, <a href="internalframe.html">internal frame</a>, or <a href="rootpane.html">root pane</a>. </td> </tr></table><p><table border=1><caption><a name="menuapi">Creating and Populating Menus</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/JMenu.html#JMenu()">JMenu()</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JMenu.html#JMenu(java.lang.String)">JMenu(String)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JMenu.html#JMenu(java.lang.Action)">JMenu(Action)</a> </td> <td>Creates a menu. The string specifies the text to display for the menu. The <code>Action</code> specifies the text and other properties of the menu (see<a class="TutorialLink" target="_top" href="../misc/action.html">How to Use Actions</a>). </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JMenu.html#add(javax.swing.JMenuItem)">JMenuItem add(JMenuItem)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JMenu.html#add(java.lang.String)">JMenuItem add(String)</a> </td> <td>Adds a menu item to the current end of the menu. If the argument is a string, then the menu automatically creates a <code>JMenuItem</code> object that displays the specified text. <p><strong>Version Note:</strong> Before 1.3, the only way to associate an <code>Action</code> with a menu item was to use menu's <code>add(Action)</code> method to create the menu item and add it to the menu. As of 1.3, that method is no longer recommended. You can instead associate a menu item with an <code>Action</code> using the <code>setAction</code> method. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JMenu.html#addSeparator()">void addSeparator()</a> </td> <td>Adds a separator to the current end of the menu. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JMenu.html#insert(javax.swing.JMenuItem, int)">JMenuItem insert(JMenuItem, int)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JMenu.html#insert(java.lang.String, int)">void insert(String, int)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JMenu.html#insertSeparator(int)">void insertSeparator(int)</a> </td> <td>Inserts a menu item or separator 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>JMenuItem</code> and <code>String</code> arguments are treated the same as in the corresponding <code>add</code> methods. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JMenu.html#remove(javax.swing.JMenuItem)">void remove(JMenuItem)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JMenu.html#remove(int)">void remove(int)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JMenu.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></table><p><table border=1><caption><a name="popupapi">Creating, Populating, and Controlling Popup Menus</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/JPopupMenu.html#JPopupMenu()">JPopupMenu()</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JPopupMenu.html#JPopupMenu(java.lang.String)">JPopupMenu(String)</a> </td> <td>Creates a popup menu. The optional string argument specifies the title that a look and feel might display as part of the popup window. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JPopupMenu.html#add(javax.swing.JMenuItem)">JMenuItem add(JMenuItem)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JPopupMenu.html#add(java.lang.String)">JMenuItem add(String)</a> </td> <td>Adds a menu item to the current end of the popup menu. If the argument is a string, then the menu automatically creates a <code>JMenuItem</code> object that displays the specified text. <p><strong>Version Note:</strong> Before 1.3, the only way to associate an <code>Action</code> with an item in a popup menu was to use the popup menu's <code>add(Action)</code> method to create the menu item and add it to the popup menu. As of 1.3, that method is no longer recommended. You can instead associate a menu item with an <code>Action</code> using the <code>setAction</code> method. </td> </tr> <tr>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -