public class MenuItem extends MenuComponent implements Accessible
MenuItem
,或它的一个子类。
默认MenuItem
对象体现了一个简单的标记菜单项。
这张图片的一个菜单栏显示五个菜单项:
前面两项简单的菜单项,标记"Basic"
和"Simple"
。以下这两项是一个分隔符,这本身就是一个菜单项,与标签"-"
创建。接下来是CheckboxMenuItem
标记"Check"
实例。最后的菜单项的子菜单和子菜单"More Examples"
标记,这是Menu
实例。
当菜单项被选中,AWT发送一个动作事件到菜单项。因为事件是ActionEvent
实例的方法,processEvent
检测事件并传递给processActionEvent
。后一种方法是将事件的已注册事件的行动菜单项产生兴趣的任何ActionListener
对象。
注意,子类Menu
重写此行为和不发送任何事件框架的一项选择之前。
Modifier and Type | Class and Description |
---|---|
protected class |
MenuItem.AccessibleAWTMenuItem
用于提供辅助支持内部类的默认菜单项。
|
MenuComponent.AccessibleAWTMenuComponent
Constructor and Description |
---|
MenuItem()
构建了一个新的菜单项和一个空的标签,没有键盘快捷键。
|
MenuItem(String label)
构建与指定的标签,没有键盘快捷键的一个新的菜单项。
|
MenuItem(String label, MenuShortcut s)
用相关的键盘快捷键创建一个菜单项。
|
Modifier and Type | Method and Description |
---|---|
void |
addActionListener(ActionListener l)
添加指定的操作侦听器从该菜单项接收操作事件。
|
void |
addNotify()
创建菜单项的对等点。
|
void |
deleteShortcut()
删除与此相关的任何
MenuShortcut 对象菜单项。
|
void |
disable()
过时的。
作为JDK 1.1版本,取而代之的
setEnabled(boolean) 。
|
protected void |
disableEvents(long eventsToDisable)
在指定的事件掩码参数定义的事件中禁用此菜单项的事件传递。
|
void |
enable()
过时的。
作为JDK 1.1版本,取而代之的
setEnabled(boolean) 。
|
void |
enable(boolean b)
过时的。
作为JDK 1.1版本,取而代之的
setEnabled(boolean) 。
|
protected void |
enableEvents(long eventsToEnable)
使用指定的事件掩码参数来定义事件传递到此菜单项中的事件
|
AccessibleContext |
getAccessibleContext()
获取与该菜单项关联的accessiblecontext。
|
String |
getActionCommand()
获取由此菜单项发射的动作事件的命令名称。
|
ActionListener[] |
getActionListeners()
返回在这个菜单项上注册的所有操作侦听器的数组。
|
String |
getLabel()
获取此菜单项的标签。
|
<T extends EventListener> |
getListeners(类<T> listenerType)
返回对象的当前注册为
FooListener s在这
MenuItem 数组。
|
MenuShortcut |
getShortcut()
得到这个菜单项关联的
MenuShortcut 对象,
|
boolean |
isEnabled()
检查此菜单项是否已启用。
|
String |
paramString()
返回一个字符串,表示此
MenuItem 状态。
|
protected void |
processActionEvent(ActionEvent e)
在此菜单项发生的过程动作事件,派遣他们到任何注册
ActionListener 对象。
|
protected void |
processEvent(AWTEvent e)
在此菜单项上处理事件。
|
void |
removeActionListener(ActionListener l)
移除指定的动作侦听器,所以它不再从该菜单项接收动作事件。
|
void |
setActionCommand(String command)
设置由此菜单项发射的动作事件的命令名。
|
void |
setEnabled(boolean b)
设置是否可以选择此菜单项。
|
void |
setLabel(String label)
将此菜单项的标签设置为指定的标签。
|
void |
setShortcut(MenuShortcut s)
设置此菜单项关联的
MenuShortcut 对象。
|
dispatchEvent, getFont, getName, getParent, getPeer, getTreeLock, postEvent, removeNotify, setFont, setName, toString
public MenuItem() throws HeadlessException
HeadlessException
-如果graphicsenvironment isheadless()返回true。
GraphicsEnvironment.isHeadless()
public MenuItem(String label) throws HeadlessException
label
-此菜单项的标签。
HeadlessException
-如果graphicsenvironment isheadless()返回true。
GraphicsEnvironment.isHeadless()
public MenuItem(String label, MenuShortcut s) throws HeadlessException
label
-此菜单项的标签。
s
-
MenuShortcut
这个菜单项关联的实例。
HeadlessException
-如果graphicsenvironment isheadless()返回true。
GraphicsEnvironment.isHeadless()
public void addNotify()
public String getLabel()
null
如果该菜单项没有标签。
setLabel(java.lang.String)
public void setLabel(String label)
label
-新的标签,无标签或
null
。
getLabel()
public boolean isEnabled()
setEnabled(boolean)
public void setEnabled(boolean b)
b
-如果
true
,使该菜单项;如果
false
,禁用它。
isEnabled()
@Deprecated public void enable()
setEnabled(boolean)
。
@Deprecated public void enable(boolean b)
setEnabled(boolean)
。
@Deprecated public void disable()
setEnabled(boolean)
。
public MenuShortcut getShortcut()
MenuShortcut
对象,
null
如果没有被指定。
setShortcut(java.awt.MenuShortcut)
public void setShortcut(MenuShortcut s)
MenuShortcut
对象。如果已与此菜单项关联的菜单快捷方式,则替换它。
s
-与此菜单项关联的菜单快捷方式。
getShortcut()
public void deleteShortcut()
MenuShortcut
对象菜单项。
protected final void enableEvents(long eventsToEnable)
从事件类型是自动启用当听众,类型添加到菜单项,这种方法只需要调用子类的MenuItem
渴望拥有指定的事件类型传递给processEvent
无论听众是注册。
eventsToEnable
的事件掩码定义的事件类型
processEvent(java.awt.AWTEvent)
,
disableEvents(long)
,
Component.enableEvents(long)
protected final void disableEvents(long eventsToDisable)
eventsToDisable
的事件掩码定义的事件类型
processEvent(java.awt.AWTEvent)
,
enableEvents(long)
,
Component.disableEvents(long)
public void setActionCommand(String command)
默认情况下,操作命令将设置为菜单项的标签。
command
要设置该菜单项的命令。
getActionCommand()
public String getActionCommand()
setActionCommand(java.lang.String)
public void addActionListener(ActionListener l)
指对AWT的线程模型的细节AWT Threading Issues。
l
的监听行动。
removeActionListener(java.awt.event.ActionListener)
,
getActionListeners()
,
ActionEvent
,
ActionListener
public void removeActionListener(ActionListener l)
指对AWT的线程模型的细节AWT Threading Issues。
l
的监听行动。
addActionListener(java.awt.event.ActionListener)
,
getActionListeners()
,
ActionEvent
,
ActionListener
public ActionListener[] getActionListeners()
ActionListener
s或空数组如果没有行动的听众正在注册
addActionListener(java.awt.event.ActionListener)
,
removeActionListener(java.awt.event.ActionListener)
,
ActionEvent
,
ActionListener
public <T extends EventListener> T[] getListeners(类<T> listenerType)
FooListener
s在这
MenuItem
数组。
FooListener
s使用
addFooListener
方法注册。如果没有这样
您可以指定一个类的listenerType
字面的说法,如FooListener.class
。例如,你可以查询其监听行动与下面的代码MenuItem
m
:
ALS功能[ ] =(action [ ])(m.getlisteners(action。类));听众的存在,此方法将返回空数组。
listenerType
-听众的类型要求;这个参数应该指定一个接口,从
java.util.EventListener
FooListener
s数组,或一个空数组如果没有这样的听众已添加
ClassCastException
-如果
listenerType
不指定一个类或接口实现
java.util.EventListener
getActionListeners()
protected void processEvent(AWTEvent e)
ActionEvent
实例,它调用
processActionEvent
,
MenuItem
定义的另一种方法。
目前,菜单项只支持动作事件。
注意,如果参数是null
行为是不可能导致异常。
processEvent
方法重写,继承类
MenuComponent
e
-事件
processActionEvent(java.awt.event.ActionEvent)
protected void processActionEvent(ActionEvent e)
ActionListener
对象。此方法不被调用,除非此组件启用了动作事件。动作事件启用时,发生以下情况之一:
ActionListener
对象是通过addActionListener
注册。enableEvents
。注意,如果参数是null
行为是不可能导致异常。
e
-动作事件
ActionEvent
,
ActionListener
,
enableEvents(long)
public String paramString()
MenuItem
状态。这种方法的目的是用于调试目的,其含量和返回的字符串格式不同的实现可能会有所不同。返回的字符串是空的但可能不
null
。
paramString
方法重写,继承类
MenuComponent
public AccessibleContext getAccessibleContext()
getAccessibleContext
接口
Accessible
getAccessibleContext
方法重写,继承类
MenuComponent
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.