📄 jmenu.java
字号:
* @param x x-coordinate of the menu location * @param y y-coordinate of the menu location */ public void setMenuLocation(int x, int y) { menuLocation = new Point(x, y); } /** * Creates and returns JMenuItem associated with the given action * * @param action Action to use for creation of JMenuItem * * @return JMenuItem that was creted with given action */ protected JMenuItem createActionComponent(Action action) { return new JMenuItem(action); } /** * Creates ActionChangeListener to listen for PropertyChangeEvents occuring * in the action that is associated with this menu * * @param item menu that contains action to listen to * * @return The PropertyChangeListener */ protected PropertyChangeListener createActionChangeListener(JMenuItem item) { return new ActionChangedListener(item); } /** * Adds separator to the end of the menu items in the menu. */ public void addSeparator() { getPopupMenu().addSeparator(); } /** * Inserts separator in the menu at the specified index. * * @param index Index at which separator should be inserted */ public void insertSeparator(int index) { if (index < 0) throw new IllegalArgumentException("index less than 0"); getPopupMenu().insert(new JPopupMenu.Separator(), index); } /** * Returns menu item located at the specified index in the menu * * @param index Index at which to look for the menu item * * @return menu item located at the specified index in the menu */ public JMenuItem getItem(int index) { if (index < 0) throw new IllegalArgumentException("index less than 0"); Component c = popupMenu.getComponentAtIndex(index); if (c instanceof JMenuItem) return (JMenuItem) c; else return null; } /** * Returns number of items in the menu including separators. * * @return number of items in the menu * * @see #getMenuComponentCount() */ public int getItemCount() { return getMenuComponentCount(); } /** * Checks if this menu is a tear-off menu. * * @return true if this menu is a tear-off menu and false otherwise */ public boolean isTearOff() { // NOT YET IMPLEMENTED return false; } /** * Returns number of menu components in this menu * * @return number of menu components in this menu */ public int getMenuComponentCount() { return popupMenu.getComponentCount(); } /** * Returns menu component located at the givent index * in the menu * * @param index index at which to get the menu component in the menu * * @return Menu Component located in the menu at the specified index */ public Component getMenuComponent(int index) { return (Component) popupMenu.getComponentAtIndex(index); } /** * Return components belonging to this menu * * @return components belonging to this menu */ public Component[] getMenuComponents() { return popupMenu.getComponents(); } /** * Checks if this menu is a top level menu. The menu is top * level menu if it is inside the menu bar. While if the menu * inside some other menu, it is considered to be a pull-right menu. * * @return true if this menu is top level menu, and false otherwise */ public boolean isTopLevelMenu() { return getParent() instanceof JMenuBar; } /** * Checks if given component exists in this menu. The submenus of * this menu are checked as well * * @param component Component to look for * * @return true if the given component exists in this menu, and false otherwise */ public boolean isMenuComponent(Component component) { return false; } /** * Returns popup menu associated with the menu. * * @return popup menu associated with the menu. */ public JPopupMenu getPopupMenu() { return popupMenu; } /** * Adds MenuListener to the menu * * @param listener MenuListener to add */ public void addMenuListener(MenuListener listener) { listenerList.add(MenuListener.class, listener); } /** * Removes MenuListener from the menu * * @param listener MenuListener to remove */ public void removeMenuListener(MenuListener listener) { listenerList.remove(MenuListener.class, listener); } /** * Returns all registered <code>MenuListener</code> objects. * * @return an array of listeners * * @since 1.4 */ public MenuListener[] getMenuListeners() { return (MenuListener[]) listenerList.getListeners(MenuListener.class); } /** * This method fires MenuEvents to all menu's MenuListeners. In this case * menuSelected() method of MenuListeners is called to indicated that the menu * was selected. */ protected void fireMenuSelected() { MenuListener[] listeners = getMenuListeners(); for (int index = 0; index < listeners.length; ++index) listeners[index].menuSelected(menuEvent); } /** * This method fires MenuEvents to all menu's MenuListeners. In this case * menuDeselected() method of MenuListeners is called to indicated that the menu * was deselected. */ protected void fireMenuDeselected() { EventListener[] ll = listenerList.getListeners(MenuListener.class); for (int i = 0; i < ll.length; i++) ((MenuListener) ll[i]).menuDeselected(menuEvent); } /** * This method fires MenuEvents to all menu's MenuListeners. In this case * menuSelected() method of MenuListeners is called to indicated that the menu * was cancelled. The menu is cancelled when it's popup menu is close without selection. */ protected void fireMenuCanceled() { EventListener[] ll = listenerList.getListeners(MenuListener.class); for (int i = 0; i < ll.length; i++) ((MenuListener) ll[i]).menuCanceled(menuEvent); } /** * Creates WinListener that listens to the menu;s popup menu. * * @param popup JPopupMenu to listen to * * @return The WinListener */ protected WinListener createWinListener(JPopupMenu popup) { return new WinListener(popup); } /** * Method of the MenuElementInterface. It reacts to the selection * changes in the menu. If this menu was selected, then it * displayes popup menu associated with it and if this menu was * deselected it hides the popup menu. * * @param changed true if the menu was selected and false otherwise */ public void menuSelectionChanged(boolean changed) { // if this menu selection is true, then activate this menu and // display popup associated with this menu setSelectedHelper(changed, isEnabled(), true); } /** * Method of MenuElement interface. Returns sub components of * this menu. * * @return array containing popupMenu that is associated with this menu */ public MenuElement[] getSubElements() { return new MenuElement[] { popupMenu }; } /** * @return Returns reference to itself */ public Component getComponent() { return this; } /** * This method is overriden with empty implementation, s.t the * accelerator couldn't be set for the menu. The mnemonic should * be used for the menu instead. * * @param keystroke accelerator for this menu */ public void setAccelerator(KeyStroke keystroke) { throw new Error("setAccelerator() is not defined for JMenu. Use setMnemonic() instead."); } /** * This method process KeyEvent occuring when the menu is visible * * @param event The KeyEvent */ protected void processKeyEvent(KeyEvent event) { // TODO: Implement this properly. } /** * Programatically performs click * * @param time Number of milliseconds for which this menu stays pressed */ public void doClick(int time) { getModel().setArmed(true); getModel().setPressed(true); try { java.lang.Thread.sleep(time); } catch (java.lang.InterruptedException e) { // probably harmless } getModel().setPressed(false); getModel().setArmed(false); popupMenu.show(this, this.getWidth(), 0); } /** * A string that describes this JMenu. Normally only used * for debugging. * * @return A string describing this JMenu */ protected String paramString() { return super.paramString(); } public AccessibleContext getAccessibleContext() { if (accessibleContext == null) accessibleContext = new AccessibleJMenu(); return accessibleContext; } // FIXME: This inner class is a complete stub and needs to be implemented. protected class AccessibleJMenu extends AccessibleJMenuItem implements AccessibleSelection { private static final long serialVersionUID = -8131864021059524309L; protected AccessibleJMenu() { // Nothing to do here. } public int getAccessibleChildrenCount() { return 0; } public Accessible getAccessibleChild(int value0) { return null; } public AccessibleSelection getAccessibleSelection() { return null; } public Accessible getAccessibleSelection(int value0) { return null; } public boolean isAccessibleChildSelected(int value0) { return false; } public AccessibleRole getAccessibleRole() { return AccessibleRole.MENU; } public int getAccessibleSelectionCount() { return 0; } public void addAccessibleSelection(int value0) { // TODO: Implement this properly. } public void removeAccessibleSelection(int value0) { // TODO: Implement this properly. } public void clearAccessibleSelection() { // TODO: Implement this properly. } public void selectAllAccessibleSelection() { // TODO: Implement this properly. } } protected class WinListener extends WindowAdapter implements Serializable { private static final long serialVersionUID = -6415815570638474823L; /** * Creates a new <code>WinListener</code>. * * @param popup the popup menu which is observed */ public WinListener(JPopupMenu popup) { // TODO: What should we do with the popup argument? } /** * Receives notification when the popup menu is closing and deselects * the menu. * * @param event the window event */ public void windowClosing(WindowEvent event) { setSelected(false); } } /** * This class listens to PropertyChangeEvents occuring in menu's action */ protected class ActionChangedListener implements PropertyChangeListener { /** menu item associated with the action */ private JMenuItem menuItem; /** Creates new ActionChangedListener and adds it to menuItem's action */ public ActionChangedListener(JMenuItem menuItem) { this.menuItem = menuItem; Action a = menuItem.getAction(); if (a != null) a.addPropertyChangeListener(this); } /**This method is invoked when some change occures in menuItem's action*/ public void propertyChange(PropertyChangeEvent evt) { // FIXME: Need to implement } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -