java.lang.Object
java.awt.Component
java.awt.Choice
- 所有已实现的接口:
ImageObserver,ItemSelectable,MenuContainer,Serializable,Accessible
Choice 类提供了一个弹出式选择菜单。当前选择显示为菜单的标题。
以下代码示例生成一个弹出菜单:
Choice ColorChooser = new Choice();
ColorChooser.add("Green");
ColorChooser.add("Red");
ColorChooser.add("Blue");
将此选择菜单添加到面板后,其正常状态如下所示:

图中,"Green"为当前选择。在对象上按下鼠标按钮会出现一个菜单,其中突出显示当前选项。
某些本机平台不支持任意调整 Choice 组件的大小,setSize()/getSize() 的行为受此类限制的约束。本机 GUI Choice 组件的大小通常受 Choice 中包含的项目的字体大小和长度等属性的约束。
- 自从:
- 1.0
- 参见:
-
内部类总结
内部类 -
字段摘要
在类 java.awt.Component 中声明的字段
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT在接口 java.awt.image.ImageObserver 中声明的字段
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH -
构造方法总结
构造方法 -
方法总结
修饰符和类型方法描述void向此Choice菜单添加一个项目。void自 Java 2 平台 v1.1 起已过时。void添加指定的项目监听器以接收来自此Choice菜单的项目事件。void创建Choice的对等体。int已弃用。获取与此Choice关联的AccessibleContext。getItem(int index) 获取此Choice菜单中指定索引处的字符串。int返回此Choice菜单中的项目数。返回在此选择上注册的所有项目监听的数组。<T extends EventListener>
T[]getListeners(Class<T> listenerType) 返回当前在此Choice上注册为FooListener的所有对象的数组。int返回当前所选项目的索引。获取当前选择的字符串表示形式。Object[]返回包含当前所选项目的数组(长度为 1)。void将项目插入此选项的指定位置。protected String返回表示此Choice菜单状态的字符串。protected void处理此选择的事件。protected void通过将它们分派到任何已注册的ItemListener对象来处理在此Choice菜单上发生的项目事件。voidremove(int position) 从指定位置的选择菜单中删除一个项目。void从Choice菜单中删除第一次出现的item。void从选择菜单中删除所有项目。void删除指定的项目监听器,以便它不再接收来自此Choice菜单的项目事件。voidselect(int pos) 将此Choice菜单中的所选项目设置为指定位置的项目。void将此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, 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, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, revalidate, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setMixingCutoutShape, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, update, validate
-
构造方法详细信息
-
Choice
创建一个新的选择菜单。菜单最初没有任何项目。默认情况下,添加到选择菜单的第一个项目成为所选项目,直到用户通过调用
select方法之一做出不同的选择。- 抛出:
HeadlessException- 如果 GraphicsEnvironment.isHeadless() 返回 true- 参见:
-
-
方法详情
-
addNotify
public void addNotify()创建Choice的对等体。这个对等点允许我们在不改变其功能的情况下改变Choice的外观。 -
getItemCount
public int getItemCount()返回此Choice菜单中的项目数。- 返回:
-
此
Choice菜单中的项目数 - 自从:
- 1.1
- 参见:
-
countItems
已弃用。从 JDK 1.1 版开始,由getItemCount()取代。返回此Choice菜单中的项目数。- 返回:
-
此
Choice菜单中的项目数
-
getItem
获取此Choice菜单中指定索引处的字符串。- 参数:
index- 开始的索引- 返回:
- 指定索引处的项目
- 参见:
-
add
向此Choice菜单添加一个项目。- 参数:
item- 要添加的项目- 抛出:
NullPointerException- 如果项目的值为null- 自从:
- 1.1
-
addItem
自 Java 2 平台 v1.1 起已过时。请改用add方法。向此
Choice菜单添加一个项目。- 参数:
item- 要添加的项目- 抛出:
NullPointerException- 如果项目的值等于null
-
insert
将项目插入此选项的指定位置。索引大于或等于index的现有项目向上移动一位以容纳新项目。如果index大于或等于此选择中的项目数,则将item添加到此选择的末尾。如果该项目是第一个添加到选择中的项目,则该项目将被选中。否则,如果所选项目是移动的项目之一,则选择中的第一个项目成为所选项目。如果所选择的项目不在那些转移的项目中,它仍然是所选择的项目。
- 参数:
item- 要插入的非null项index- 应插入项目的位置- 抛出:
IllegalArgumentException- 如果索引小于 0
-
remove
从Choice菜单中删除第一次出现的item。如果要删除的项目是当前选定的项目,则选择中的第一项将成为选定的项目。否则,当前选中的项目保持选中状态(并且选中的索引会相应更新)。- 参数:
item- 要从此Choice菜单中删除的项目- 抛出:
IllegalArgumentException- 如果该项目在选择菜单中不存在- 自从:
- 1.1
-
remove
public void remove(int position) 从指定位置的选择菜单中删除一个项目。如果要删除的项目是当前选定的项目,则选择中的第一项将成为选定的项目。否则,当前选中的项目保持选中状态(并且选中的索引会相应更新)。- 参数:
position- 项目的位置- 抛出:
IndexOutOfBoundsException- 如果指定位置越界- 自从:
- 1.1
-
removeAll
public void removeAll()从选择菜单中删除所有项目。- 自从:
- 1.1
- 参见:
-
getSelectedItem
获取当前选择的字符串表示形式。- 返回:
- 此选择菜单中当前所选项目的字符串表示形式
- 参见:
-
getSelectedObjects
返回包含当前所选项目的数组(长度为 1)。如果此选择没有项目,则返回null。- 指定者:
getSelectedObjects在接口ItemSelectable中- 返回:
-
选定对象的列表,或
null - 参见:
-
getSelectedIndex
public int getSelectedIndex()返回当前所选项目的索引。如果未选择任何内容,则返回 -1。- 返回:
- 当前所选项目的索引,如果当前未选择任何内容,则为 -1
- 参见:
-
select
public void select(int pos) 将此Choice菜单中的所选项目设置为指定位置的项目。请注意,此方法应该主要用于初始选择此组件中的项目。以编程方式调用此方法将not触发
ItemEvent。触发ItemEvent的唯一方法是通过用户交互。- 参数:
pos- 所选项目的位置- 抛出:
IllegalArgumentException- 如果指定位置大于项目数或小于零- 参见:
-
select
将此Choice菜单中的所选项目设置为其名称等于指定字符串的项目。如果有多个项目匹配(等于)指定的字符串,则选择具有最小索引的项目。请注意,此方法应该主要用于初始选择此组件中的项目。以编程方式调用此方法将not触发
ItemEvent。触发ItemEvent的唯一方法是通过用户交互。- 参数:
str- 指定的字符串- 参见:
-
addItemListener
添加指定的项目监听器以接收来自此Choice菜单的项目事件。发送项目事件以响应用户输入,但不响应对select的调用。如果 l 是null,则不会抛出异常并且不会执行任何操作。有关 AWT 线程模型的详细信息,请参阅 AWT 线程问题。
- 指定者:
addItemListener在接口ItemSelectable中- 参数:
l- 项目监听器- 自从:
- 1.1
- 参见:
-
removeItemListener
- 指定者:
removeItemListener在接口ItemSelectable中- 参数:
l- 项目监听器- 自从:
- 1.1
- 参见:
-
getItemListeners
返回在此选择上注册的所有项目监听的数组。- 返回:
-
所有这个选择的
ItemListeners 或一个空数组,如果当前没有注册项目监听器 - 自从:
- 1.4
- 参见:
-
getListeners
返回当前在此Choice上注册为FooListener的所有对象的数组。FooListener是使用addFooListener方法注册的。您可以使用类文字指定
listenerType参数,例如FooListener.class。例如,您可以使用以下代码查询Choice c的项目监听器:ItemListener[] ils = (ItemListener[])(c.getListeners(ItemListener.class));
如果不存在这样的监听器,则此方法返回一个空数组。- 重写:
getListeners在类Component中- 类型参数:
T- 监听器的类型- 参数:
listenerType- 请求的监听器类型;此参数应指定从java.util.EventListener派生的接口- 返回:
-
在此选择上注册为
FooListener的所有对象的数组,如果没有添加此类监听器,则为空数组 - 抛出:
ClassCastException- 如果listenerType没有指定实现java.util.EventListener的类或接口- 自从:
- 1.3
- 参见:
-
processEvent
处理此选择的事件。如果事件是ItemEvent的实例,它会调用processItemEvent方法。否则,它调用其超类的processEvent方法。请注意,如果事件参数为
null,则行为未指定且可能导致异常。- 重写:
processEvent在类Component中- 参数:
e- 事件- 自从:
- 1.1
- 参见:
-
processItemEvent
通过将它们分派到任何已注册的ItemListener对象来处理在此Choice菜单上发生的项目事件。除非为此组件启用项目事件,否则不会调用此方法。当发生以下情况之一时,将启用项目事件:
ItemListener对象通过addItemListener注册。- 项目事件通过
enableEvents启用。
请注意,如果事件参数为
null,则行为未指定且可能导致异常。- 参数:
e- 项目事件- 自从:
- 1.1
- 参见:
-
paramString
返回表示此Choice菜单状态的字符串。此方法仅用于调试目的,返回字符串的内容和格式可能因实现而异。返回的字符串可能为空,但可能不是null。- 重写:
paramString在类Component中- 返回:
-
这个
Choice菜单的参数字符串
-
getAccessibleContext
获取与此Choice关联的AccessibleContext。对于Choice组件,AccessibleContext采用AccessibleAWTChoice的形式。如有必要,将创建一个新的AccessibleAWTChoice实例。- 指定者:
getAccessibleContext在接口Accessible中- 重写:
getAccessibleContext在类Component中- 返回:
-
一个
AccessibleAWTChoice作为这个Choice的AccessibleContext - 自从:
- 1.3
-
getItemCount()取代。