JavaTM 2 Platform
Standard Ed. 6

java.awt
类 Choice

java.lang.Object
  继承者 java.awt.Component
      继承者 java.awt.Choice
所有已实现的接口:
ImageObserver, ItemSelectable, MenuContainer, Serializable, Accessible

public class Choice
extends Component
implements ItemSelectable, Accessible

Choice 类表示一个弹出式选择菜单。当前的选择显示为菜单的标题。

下列代码示例产生了一个弹出式菜单:


Choice ColorChooser = new Choice();
ColorChooser.add("Green");
ColorChooser.add("Red");
ColorChooser.add("Blue");

在将此选择菜单添加到一个面板中后,它显示为以下正常状态:

以下文本描述了该图形

在这个图像中,"Green" 为当前选择。在对象上按下鼠标按键,这将显示一个菜单,菜单的当前选择被高亮显示。

一些本机平台不支持任意调整 Choice 组件的大小,setSize()/getSize() 的行为受到这类限制的约束。本机 GUI Choice 组件的大小常受到一些属性的限制,比如字体大小和 Choice 中包含的项的长度。

从以下版本开始:
JDK1.0
另请参见:
序列化表格

嵌套类摘要
protected  class Choice.AccessibleAWTChoice
          此类实现对 Choice 类的可访问性支持。
 
从类 java.awt.Component 继承的嵌套类/接口
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
字段摘要
 
从类 java.awt.Component 继承的字段
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
从接口 java.awt.image.ImageObserver 继承的字段
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
构造方法摘要
Choice()
          创建一个新的选择菜单。
 
方法摘要
 void add(String item)
          将一个项添加到此 Choice 菜单中。
 void addItem(String item)
          如果对于 Java 2 platform v1.1 已过时,请使用 add 方法替换。
 void addItemListener(ItemListener l)
          添加指定的项侦听器,以接收发自此 Choice 菜单的项事件。
 void addNotify()
          创建 Choice 的同位体。
 int countItems()
          已过时。 从 JDK version 1.1 开始,由 getItemCount() 取代。
 AccessibleContext getAccessibleContext()
          获取与此 Choice 相关的 AccessibleContext
 String getItem(int index)
          获取此 Choice 菜单中指定索引上的字符串。
 int getItemCount()
          返回此 Choice 菜单中项的数量。
 ItemListener[] getItemListeners()
          返回已在此选择上注册的所有项侦听器组成的数组。
<T extends EventListener>
T[]
getListeners(Class<T> listenerType)
          返回目前已在此 Choice 上注册为 FooListener 的所有对象组成的数组。
 int getSelectedIndex()
          返回当前选定项的索引。
 String getSelectedItem()
          获取当前选择的字符串表示形式。
 Object[] getSelectedObjects()
          返回包含当前选定项的数组(长度为 1)。
 void insert(String item, int index)
          将菜单项插入此选择的指定位置上。
protected  String paramString()
          返回表示此 Choice 菜单的状态的字符串。
protected  void processEvent(AWTEvent e)
          处理关于此选择的事件。
protected  void processItemEvent(ItemEvent e)
          处理发生在此 Choice 菜单上的项事件,实现方式是将这些事件指派给所有已注册的 ItemListener 对象。
 void remove(int position)
          从选择菜单的指定位置上移除一个项。
 void remove(String item)
          移除 Choice 菜单中第一个出现的 item
 void removeAll()
          从选择菜单中移除所有的项。
 void removeItemListener(ItemListener l)
          移除指定的项侦听器,以便它不再接收发自此 Choice 菜单的项事件。
 void select(int pos)
          将此 Choice 菜单中的选定项设置为指定位置上的项。
 void select(String str)
          将此 Choice 菜单中的选定项设置为其名称等于指定字符串的项。
 
从类 java.awt.Component 继承的方法
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAlignmentX, getAlignmentY, getBackground, getBaseline, getBaselineResizeBehavior, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paint, paintAll, postEvent, preferredSize, prepareImage, prepareImage, print, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, update, validate
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

构造方法详细信息

Choice

public Choice()
       throws HeadlessException
创建一个新的选择菜单。最初,此菜单中没有任何项。

默认情况下,在用户通过调用 select 方法之一进行不同的选择之前,给选择菜单添加的第一个项将成为选定项。

抛出:
HeadlessException - 如果 GraphicsEnvironment.isHeadless() 返回 true
另请参见:
GraphicsEnvironment.isHeadless(), select(int), select(java.lang.String)
方法详细信息

addNotify

public void addNotify()
创建 Choice 的同位体。此同位体允许更改 Choice 的外观,而不更改其功能。

覆盖:
Component 中的 addNotify
另请参见:
Toolkit.createChoice(java.awt.Choice), Component.getToolkit()

getItemCount

public int getItemCount()
返回此 Choice 菜单中项的数量。

返回:
Choice 菜单中项的数量
从以下版本开始:
JDK1.1
另请参见:
getItem(int)

countItems

@Deprecated
public int countItems()
已过时。 从 JDK version 1.1 开始,由 getItemCount() 取代。


getItem

public String getItem(int index)
获取此 Choice 菜单中指定索引上的字符串。

参数:
index - 起始索引
另请参见:
getItemCount()

add

public void add(String item)
将一个项添加到此 Choice 菜单中。

参数:
item - 要添加的项
抛出:
NullPointerException - 如果这个项的值为 null
从以下版本开始:
JDK1.1

addItem

public void addItem(String item)
如果对于 Java 2 platform v1.1 已过时,请使用 add 方法替换。

将一个项添加到此 Choice 菜单中。

参数:
item - 要添加的菜单项
抛出:
NullPointerException - 如果这个项的值等于 null

insert

public void insert(String item,
                   int index)
将菜单项插入此选择的指定位置上。如果某个索引上存在的项大于等于 index,则将它上移一位,以容纳新的项。如果 index 大于等于此选择中项的数目,则将 item 添加到此选择的结尾处。

如果此项是第一个添加到该选择中的项,则这个项将成为选定项。否则,如果选定项是上移项中的一个,则该选择中的第一个项将成为选定项。如果选定项不在这些上移项中,则它仍然是选定项。

参数:
item - 将插入的非 null
index - 应该插入项的位置
抛出:
IllegalArgumentException - 如果索引小于 0

remove

public void remove(String item)
移除 Choice 菜单中第一个出现的 item。如果正被移除的项是目前选定的项,则该选择中的第一个项将成为选定项。否则,当前选定项仍然是选定项(并且选定的索引被相应地更新)。

参数:
item - 将从此 Choice 菜单中移除的项
抛出:
IllegalArgumentException - 如果该项不在此选择菜单中
从以下版本开始:
JDK1.1

remove

public void remove(int position)
从选择菜单的指定位置上移除一个项。如果正被移除的项是目前选定的项,则该选择中的第一个项将成为选定项。否则,当前选定项将仍然是选定项(并且选定的索引被相应地更新)。

参数:
position - 项的位置
抛出:
IndexOutOfBoundsException - 如果指定位置超出边界
从以下版本开始:
JDK1.1

removeAll

public void removeAll()
从选择菜单中移除所有的项。

从以下版本开始:
JDK1.1
另请参见:
remove(java.lang.String)

getSelectedItem

public String getSelectedItem()
获取当前选择的字符串表示形式。

返回:
此选择菜单中当前选定项的字符串表示形式
另请参见:
getSelectedIndex()

getSelectedObjects

public Object[] getSelectedObjects()
返回包含当前选定项的数组(长度为 1)。如果此选择菜单中没项,则返回 null

指定者:
接口 ItemSelectable 中的 getSelectedObjects
另请参见:
ItemSelectable

getSelectedIndex

public int getSelectedIndex()
返回当前选定项的索引。如果没有选定任何内容,则返回 -1。

返回:
当前选定项的索引,如果目前没有选定任何内容,则返回 -1
另请参见:
getSelectedItem()

select

public void select(int pos)
将此 Choice 菜单中的选定项设置为指定位置上的项。

注意,此方法主要应该用于对此组件中某个项的选择的初始化。以编程方式调用此方法 会触发 ItemEvent。触发 ItemEvent 的唯一方式是通过用户交互。

参数:
pos - 指定项的位置
抛出:
IllegalArgumentException - 如果指定位置大于项的数量或小于零
另请参见:
getSelectedItem(), getSelectedIndex()

select

public void select(String str)
将此 Choice 菜单中的选定项设置为其名称等于指定字符串的项。如果有多个项与指定的字符串匹配(相等),则选择具有最小索引的那一个项。

注意,此方法主要应该用于初始化对此组件中某个项的选择。以编程方式调用此方法 会触发 ItemEvent。触发 ItemEvent 的唯一方式是通过用户交互。

参数:
str - 指定的字符串
另请参见:
getSelectedItem(), getSelectedIndex()

addItemListener

public void addItemListener(ItemListener l)
添加指定的项侦听器,以接收发自此 Choice 菜单的项事件。通过发送项事件来响应用户输入,但不响应对 select 的调用。如果 l 为 null,则不会抛出异常,并且不执行任何动作。有关 AWT 的线程模型的细节信息,请参阅 AWT 线程问题

指定者:
接口 ItemSelectable 中的 addItemListener
参数:
l - 项侦听器
从以下版本开始:
JDK1.1
另请参见:
removeItemListener(java.awt.event.ItemListener), getItemListeners(), select(int), ItemEvent, ItemListener

removeItemListener

public void removeItemListener(ItemListener l)
移除指定的项侦听器,以便它不再接收发自此 Choice 菜单的项事件。如果 l 为 null,则不会抛出异常,并且不执行任何动作。有关 AWT 的线程模型的细节信息,请参阅 AWT 线程问题

指定者:
接口 ItemSelectable 中的 removeItemListener
参数:
l - 项侦听器
从以下版本开始:
JDK1.1
另请参见:
addItemListener(java.awt.event.ItemListener), getItemListeners(), ItemEvent, ItemListener

getItemListeners

public ItemListener[] getItemListeners()
返回已在此选择上注册的所有项侦听器组成的数组。

返回:
此选择的所有 ItemListener,如果没有当前已注册的项侦听器,则返回一个空数组
从以下版本开始:
1.4
另请参见:
addItemListener(java.awt.event.ItemListener), removeItemListener(java.awt.event.ItemListener), ItemEvent, ItemListener

getListeners

public <T extends EventListener> T[] getListeners(Class<T> listenerType)
返回目前已在此 Choice 上注册为 FooListener 的所有对象组成的数组。FooListener 是使用 addFooListener 方法注册的。

可以用一个 class 字面值(比如 FooListener.class)指定 listenerType 参数。例如,可以使用以下代码查询 Choice c,以获得它的项侦听器:

ItemListener[] ils = (ItemListener[])(c.getListeners(ItemListener.class));
如果不存在这样的侦听器,则此方法将返回一个空数组。

覆盖:
Component 中的 getListeners
参数:
listenerType - 所请求侦听器的类型;该参数应该指定一个从 java.util.EventListener 遗传下来的接口
返回:
在此选择上注册为 FooListener 的所有对象组成的数组,如果没有添加这样的侦听器,则返回一个空数组
抛出:
ClassCastException - 如果 listenerType 没有指定实现 java.util.EventListener 的类或接口
从以下版本开始:
1.3
另请参见:
getItemListeners()

processEvent

protected void processEvent(AWTEvent e)
处理关于此选择的事件。如果事件是 ItemEvent 的一个实例,则此方法将调用 processItemEvent 方法。否则,它将调用其超类的 processEvent 方法。

注意,如果事件参数为 null,则行为是不确定的,并且可能导致异常。

覆盖:
Component 中的 processEvent
参数:
e - 事件
从以下版本开始:
JDK1.1
另请参见:
ItemEvent, processItemEvent(java.awt.event.ItemEvent)

processItemEvent

protected void processItemEvent(ItemEvent e)
处理发生在此 Choice 菜单上的项事件,实现方式是将这些事件指派给所有已注册的 ItemListener 对象。

只有为此组件启用项事件之后,才调用此方法。项事件是在出现以下情况之一时启用:

注意,如果事件参数为 null,则行为是不确定的,并且可能导致异常。

参数:
e - 项事件
从以下版本开始:
JDK1.1
另请参见:
ItemEvent, ItemListener, addItemListener(ItemListener), Component.enableEvents(long)

paramString

protected String paramString()
返回表示此 Choice 菜单的状态的字符串。此方法仅在进行调试的时候使用,对于这两个实现,所返回字符串的内容和格式可能有所不同。返回的字符串可能为空,但不可能为 null

覆盖:
Component 中的 paramString
返回:
Choice 菜单的参数字符串

getAccessibleContext

public AccessibleContext getAccessibleContext()
获取与此 Choice 相关的 AccessibleContext。对于 Choice 组件,AccessibleContext 采用的是 AccessibleAWTChoice 的形式。如有必要,可以创建一个新的 AccessibleAWTChoice 实例。

指定者:
接口 Accessible 中的 getAccessibleContext
覆盖:
Component 中的 getAccessibleContext
返回:
充当此 ChoiceAccessibleContextAccessibleAWTChoice
从以下版本开始:
1.3

JavaTM 2 Platform
Standard Ed. 6

提交错误或意见

版权所有 2008 Sun Microsystems, Inc. 保留所有权利。请遵守GNU General Public License, version 2 only