📄 menuitem.java
字号:
/*
* @(#)MenuItem.java 1.48 98/08/21
*
* Copyright 1995-1998 by Sun Microsystems, Inc.,
* 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
* All rights reserved.
*
* This software is the confidential and proprietary information
* of Sun Microsystems, Inc. ("Confidential Information"). You
* shall not disclose such Confidential Information and shall use
* it only in accordance with the terms of the license agreement
* you entered into with Sun.
*/
package java.awt;
import java.awt.peer.MenuItemPeer;
import java.awt.event.*;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.IOException;
/**
* All items in a menu must belong to the class
* <code>MenuItem</code>, or one of its subclasses.
* <p>
* The default <code>MenuItem</code> object embodies
* a simple labeled menu item.
* <p>
* This picture of a menu bar shows five menu items:
* <IMG SRC="images-awt/MenuBar-1.gif"
* ALIGN=CENTER HSPACE=10 VSPACE=7>
* <br CLEAR=LEFT>
* The first two items are simple menu items, labeled
* <code>"Basic"</code> and <code>"Simple"</code>.
* Following these two items is a separator, which is itself
* a menu item, created with the label <code>"-"</code>.
* Next is an instance of <code>CheckboxMenuItem</code>
* labeled <code>"Check"</code>. The final menu item is a
* submenu labeled <code>"More Examples"</code>,
* and this submenu is an instance of <code>Menu</code>.
* <p>
* When a menu item is selected, AWT sends an action event to
* the menu item. Since the event is an
* instance of <code>ActionEvent</code>, the <code>processEvent</code>
* method examines the event and passes it along to
* <code>processActionEvent</code>. The latter method redirects the
* event to any <code>ActionListener</code> objects that have
* registered an interest in action events generated by this
* menu item.
* <P>
* Note that the subclass <code>Menu</code> overrides this behavior and
* does not send any event to the frame until one of its subitems is
* selected.
*
* @version 1.48, 08/21/98
* @author Sami Shaio
*/
public class MenuItem extends MenuComponent {
boolean enabled = true;
String label;
String actionCommand;
// The eventMask is ONLY set by subclasses via enableEvents.
// The mask should NOT be set when listeners are registered
// so that we can distinguish the difference between when
// listeners request events and subclasses request them.
long eventMask;
transient ActionListener actionListener;
private MenuShortcut shortcut = null;
private static final String base = "menuitem";
private static int nameCounter = 0;
/*
* JDK 1.1 serialVersionUID
*/
private static final long serialVersionUID = -21757335363267194L;
/**
* Constructs a new MenuItem with an empty label and no keyboard
* shortcut.
* @since JDK1.1
*/
public MenuItem() {
this("", null);
}
/**
* Constructs a new MenuItem with the specified label
* and no keyboard shortcut. Note that use of "-" in
* a label is reserved to indicate a separator between
* menu items. By default, all menu items except for
* separators are enabled.
* @param label the label for this menu item.
* @since JDK1.0
*/
public MenuItem(String label) {
this(label, null);
}
/**
* Create a menu item with an associated keyboard shortcut.
* Note that use of "-" in a label is reserved to indicate
* a separator between menu items. By default, all menu
* items except for separators are enabled.
* @param label the label for this menu item.
* @param s the instance of <code>MenuShortcut</code>
* associated with this menu item.
* @since JDK1.1
*/
public MenuItem(String label, MenuShortcut s) {
this.label = label;
this.shortcut = s;
}
/**
* Construct a name for this MenuComponent. Called by getName() when
* the name is null.
*/
String constructComponentName() {
return base + nameCounter++;
}
/**
* Creates the menu item's peer. The peer allows us to modify the
* appearance of the menu item without changing its functionality.
*/
public void addNotify() {
synchronized (getTreeLock()) {
if (peer == null) {
peer = Toolkit.getDefaultToolkit().createMenuItem(this);
}
}
}
/**
* Gets the label for this menu item.
* @return the label of this menu item, or <code>null</code>
if this menu item has no label.
* @see java.awt.MenuItem#setLabel
* @since JDK1.0
*/
public String getLabel() {
return label;
}
/**
* Sets the label for this menu item to the specified label.
* @param label the new label, or <code>null</code> for no label.
* @see java.awt.MenuItem#getLabel
* @since JDK1.0
*/
public synchronized void setLabel(String label) {
this.label = label;
MenuItemPeer peer = (MenuItemPeer)this.peer;
if (peer != null) {
peer.setLabel(label);
}
}
/**
* Checks whether this menu item is enabled.
* @see java.awt.MenuItem#setEnabled
* @since JDK1.0
*/
public boolean isEnabled() {
return enabled;
}
/**
* Sets whether or not this menu item can be chosen.
* @param b if <code>true</code>, enables this menu item;
* if <code>false</code>, disables it.
* @see java.awt.MenuItem#isEnabled
* @since JDK1.1
*/
public synchronized void setEnabled(boolean b) {
enable(b);
}
/**
* @deprecated As of JDK version 1.1,
* replaced by <code>setEnabled(boolean)</code>.
*/
public synchronized void enable() {
enabled = true;
MenuItemPeer peer = (MenuItemPeer)this.peer;
if (peer != null) {
peer.enable();
}
}
/**
* @deprecated As of JDK version 1.1,
* replaced by <code>setEnabled(boolean)</code>.
*/
public void enable(boolean b) {
if (b) {
enable();
} else {
disable();
}
}
/**
* @deprecated As of JDK version 1.1,
* replaced by <code>setEnabled(boolean)</code>.
*/
public synchronized void disable() {
enabled = false;
MenuItemPeer peer = (MenuItemPeer)this.peer;
if (peer != null) {
peer.disable();
}
}
/**
* Get the <code>MenuShortcut</code> object associated with this
* menu item,
* @return the menu shortcut associated with this menu item,
* or <code>null</code> if none has been specified.
* @see java.awt.MenuItem#setShortcut
* @since JDK1.1
*/
public MenuShortcut getShortcut() {
return shortcut;
}
/**
* Set the <code>MenuShortcut</code> object associated with this
* menu item. If a menu shortcut is already associated with
* this menu item, it is replaced.
* @param s the menu shortcut to associate
* with this menu item.
* @see java.awt.MenuItem#getShortcut
* @since JDK1.1
*/
public void setShortcut(MenuShortcut s) {
shortcut = s;
synchronized (getTreeLock()) { /*ibm.3202*/
MenuItemPeer peer = (MenuItemPeer)this.peer;
if (peer != null) {
peer.setLabel(label);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -