📄 jmenu.java
字号:
/* JMenu.java -- Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package javax.swing;import java.awt.Component;import java.awt.Point;import java.awt.event.KeyEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import java.io.Serializable;import java.util.EventListener;import javax.accessibility.Accessible;import javax.accessibility.AccessibleContext;import javax.accessibility.AccessibleRole;import javax.accessibility.AccessibleSelection;import javax.swing.event.MenuEvent;import javax.swing.event.MenuListener;import javax.swing.plaf.MenuItemUI;/** * This class represents a menu that can be added to a menu bar or * can be a submenu in some other menu. When JMenu is selected it * displays JPopupMenu containing its menu items. * * <p> * JMenu's fires MenuEvents when this menu's selection changes. If this menu * is selected, then fireMenuSelectedEvent() is invoked. In case when menu is * deselected or cancelled, then fireMenuDeselectedEvent() or * fireMenuCancelledEvent() is invoked, respectivelly. * </p> */public class JMenu extends JMenuItem implements Accessible, MenuElement{ private static final long serialVersionUID = 4227225638931828014L; /** A Popup menu associated with this menu, which pops up when menu is selected */ private JPopupMenu popupMenu = new JPopupMenu(); /** Whenever menu is selected or deselected the MenuEvent is fired to menu's registered listeners. */ private MenuEvent menuEvent = new MenuEvent(this); /*Amount of time, in milliseconds, that should pass before popupMenu associated with this menu appears or disappers */ private int delay; /* PopupListener */ protected WinListener popupListener; /** Location at which popup menu associated with this menu will be displayed */ private Point menuLocation; /** * Creates a new JMenu object. */ public JMenu() { super(); setOpaque(false); } /** * Creates a new <code>JMenu</code> with the specified label. * * @param text label for this menu */ public JMenu(String text) { super(text); popupMenu.setInvoker(this); setOpaque(false); } /** * Creates a new <code>JMenu</code> object. * * @param action Action that is used to create menu item tha will be * added to the menu. */ public JMenu(Action action) { super(action); createActionChangeListener(this); popupMenu.setInvoker(this); setOpaque(false); } /** * Creates a new <code>JMenu</code> with specified label and an option * for this menu to be tear-off menu. * * @param text label for this menu * @param tearoff true if this menu should be tear-off and false otherwise */ public JMenu(String text, boolean tearoff) { // FIXME: tearoff not implemented this(text); } /** * Adds specified menu item to this menu * * @param item Menu item to add to this menu * * @return Menu item that was added */ public JMenuItem add(JMenuItem item) { return popupMenu.add(item); } /** * Adds specified component to this menu. * * @param component Component to add to this menu * * @return Component that was added */ public Component add(Component component) { popupMenu.insert(component, -1); return component; } /** * Adds specified component to this menu at the given index * * @param component Component to add * @param index Position of this menu item in the menu * * @return Component that was added */ public Component add(Component component, int index) { return popupMenu.add(component, index); } /** * Adds JMenuItem constructed with the specified label to this menu * * @param text label for the menu item that will be added * * @return Menu Item that was added to this menu */ public JMenuItem add(String text) { return popupMenu.add(text); } /** * Adds JMenuItem constructed using properties from specified action. * * @param action action to construct the menu item with * * @return Menu Item that was added to this menu */ public JMenuItem add(Action action) { return popupMenu.add(action); } /** * Removes given menu item from this menu. Nothing happens if * this menu doesn't contain specified menu item. * * @param item Menu Item which needs to be removed */ public void remove(JMenuItem item) { popupMenu.remove(item); } /** * Removes component at the specified index from this menu * * @param index Position of the component that needs to be removed in the menu */ public void remove(int index) { popupMenu.remove(index); } /** * Removes given component from this menu. * * @param component Component to remove */ public void remove(Component component) { int index = popupMenu.getComponentIndex(component); popupMenu.remove(index); } /** * Removes all menu items from the menu */ public void removeAll() { popupMenu.removeAll(); } /** * Creates JMenuItem with the specified text and inserts it in the * at the specified index * * @param text label for the new menu item * @param index index at which to insert newly created menu item. */ public void insert(String text, int index) { this.insert(new JMenuItem(text), index); } /** * Creates JMenuItem with the specified text and inserts it in the * at the specified index. IllegalArgumentException is thrown * if index is less than 0 * * @param item menu item to insert * @param index index at which to insert menu item. * @return Menu item that was added to the menu */ public JMenuItem insert(JMenuItem item, int index) { if (index < 0) throw new IllegalArgumentException("index less than zero"); popupMenu.insert(item, index); return item; } /** * Creates JMenuItem with the associated action and inserts it to the menu * at the specified index. IllegalArgumentException is thrown * if index is less than 0 * * @param action Action for the new menu item * @param index index at which to insert newly created menu item. * @return Menu item that was added to the menu */ public JMenuItem insert(Action action, int index) { JMenuItem item = new JMenuItem(action); this.insert(item, index); return item; } /** * This method sets this menuItem's UI to the UIManager's default for the * current look and feel. */ public void updateUI() { super.setUI((MenuItemUI) UIManager.getUI(this)); invalidate(); } /** * This method returns a name to identify which look and feel class will be * the UI delegate for the menu. * * @return The Look and Feel classID. "MenuUI" */ public String getUIClassID() { return "MenuUI"; } /** * Sets model for this menu. * * @param model model to set */ public void setModel(ButtonModel model) { super.setModel(model); } /** * Returns true if the menu is selected and false otherwise * * @return true if the menu is selected and false otherwise */ public boolean isSelected() { return super.isSelected(); } /** * A helper method to handle setSelected calls from both mouse events and * direct calls to setSelected. Direct calls shouldn't expand the popup * menu and should select the JMenu even if it is disabled. Mouse events * only select the JMenu if it is enabled and should expand the popup menu * associated with this JMenu. * @param selected whether or not the JMenu was selected * @param menuEnabled whether or not selecting the menu is "enabled". This * is always true for direct calls, and is set to isEnabled() for mouse * based calls. * @param showMenu whether or not to show the popup menu */ private void setSelectedHelper(boolean selected, boolean menuEnabled, boolean showMenu) { // If menu is selected and enabled, activates the menu and // displays associated popup. if (selected && menuEnabled) { super.setArmed(true); super.setSelected(true); // FIXME: The popup menu should be shown on the screen after certain // number of seconds pass. The 'delay' property of this menu indicates // this amount of seconds. 'delay' property is 0 by default. if (isShowing()) { fireMenuSelected(); int x = 0; int y = 0; if (showMenu) if (menuLocation == null) { // Calculate correct position of the popup. Note that location of the popup // passed to show() should be relative to the popup's invoker if (isTopLevelMenu()) y = this.getHeight(); else x = this.getWidth(); getPopupMenu().show(this, x, y); } else { getPopupMenu().show(this, menuLocation.x, menuLocation.y); } } } else { super.setSelected(false); super.setArmed(false); fireMenuDeselected(); popupMenu.setVisible(false); } } /** * Changes this menu selected state if selected is true and false otherwise * This method fires menuEvents to menu's registered listeners. * * @param selected true if the menu should be selected and false otherwise */ public void setSelected(boolean selected) { setSelectedHelper(selected, true, false); } /** * Checks if PopupMenu associated with this menu is visible * * @return true if the popup associated with this menu is currently visible * on the screen and false otherwise. */ public boolean isPopupMenuVisible() { return popupMenu.isVisible(); } /** * Sets popup menu visibility * * @param popup true if popup should be visible and false otherwise */ public void setPopupMenuVisible(boolean popup) { if (getModel().isEnabled()) popupMenu.setVisible(popup); } /** * Returns origin point of the popup menu * * @return Point containing */ protected Point getPopupMenuOrigin() { // if menu in the menu bar if (isTopLevelMenu()) return new Point(0, this.getHeight()); // if submenu return new Point(this.getWidth(), 0); } /** * Returns delay property. * * @return delay property, indicating number of milliseconds before * popup menu associated with the menu appears or disappears after * menu was selected or deselected respectively */ public int getDelay() { return delay; } /** * Sets delay property for this menu. If given time for the delay * property is negative, then IllegalArgumentException is thrown * * @param delay number of milliseconds before * popup menu associated with the menu appears or disappears after * menu was selected or deselected respectively */ public void setDelay(int delay) { if (delay < 0) throw new IllegalArgumentException("delay less than 0"); this.delay = delay; } /** * Sets location at which popup menu should be displayed * The location given is relative to this menu item *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -