📄 choice.java
字号:
/* Choice.java -- Java choice button widget. Copyright (C) 1999, 2000, 2001, 2002, 2004 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package java.awt;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import java.awt.peer.ChoicePeer;import java.io.Serializable;import java.util.EventListener;import java.util.Vector;import javax.accessibility.Accessible;import javax.accessibility.AccessibleAction;import javax.accessibility.AccessibleContext;import javax.accessibility.AccessibleRole;/** * This class implements a drop down choice list. * * @author Aaron M. Renn (arenn@urbanophile.com) */public class Choice extends Component implements ItemSelectable, Serializable, Accessible{/* * Static Variables */// Serialization constantprivate static final long serialVersionUID = -4075310674757313071L;/*************************************************************************//* * Instance Variables *//** * @serial A list of items for the choice box, which can be <code>null</code>. * This is package-private to avoid an accessor method. */Vector pItems = new Vector();/** * @serial The index of the selected item in the choice box. */private int selectedIndex = -1;// Listener chainprivate ItemListener item_listeners;/** * This class provides accessibility support for the * combo box. * * @author Jerry Quinn (jlquinn@optonline.net) * @author Andrew John Hughes (gnu_andrew@member.fsf.org) */ protected class AccessibleAWTChoice extends AccessibleAWTComponent implements AccessibleAction { /** * Serialization constant to match JDK 1.5 */ private static final long serialVersionUID = 7175603582428509322L; /** * Default constructor which simply calls the * super class for generic component accessibility * handling. */ public AccessibleAWTChoice() { super(); } /** * Returns an implementation of the <code>AccessibleAction</code> * interface for this accessible object. In this case, the * current instance is simply returned (with a more appropriate * type), as it also implements the accessible action as well as * the context. * * @return the accessible action associated with this context. * @see javax.accessibility.AccessibleAction */ public AccessibleAction getAccessibleAction() { return this; } /** * Returns the role of this accessible object. * * @return the instance of <code>AccessibleRole</code>, * which describes this object. * @see javax.accessibility.AccessibleRole */ public AccessibleRole getAccessibleRole() { return AccessibleRole.COMBO_BOX; } /** * Returns the number of actions associated with this accessible * object. In this case, it is the number of choices available. * * @return the number of choices available. * @see javax.accessibility.AccessibleAction#getAccessibleActionCount() */ public int getAccessibleActionCount() { return pItems.size(); } /** * Returns a description of the action with the supplied id. * In this case, it is the text used in displaying the particular * choice on-screen. * * @param i the id of the choice whose description should be * retrieved. * @return the <code>String</code> used to describe the choice. * @see javax.accessibility.AccessibleAction#getAccessibleActionDescription(int) */ public String getAccessibleActionDescription(int i) { return (String) pItems.get(i); } /** * Executes the action with the specified id. In this case, * calling this method provides the same behaviour as would * choosing a choice from the list in a visual manner. * * @param i the id of the choice to select. * @return true if a valid choice was specified. * @see javax.accessibility.AccessibleAction#doAccessibleAction(int) */ public boolean doAccessibleAction(int i) { if (i < 0 || i >= pItems.size()) return false; Choice.this.processItemEvent(new ItemEvent(Choice.this, ItemEvent.ITEM_STATE_CHANGED, this, ItemEvent.SELECTED)); return true; } }/*************************************************************************//* * Constructors */ /** * Initializes a new instance of <code>Choice</code>. * * @exception HeadlessException If GraphicsEnvironment.isHeadless() * returns true */ public Choice() { if (GraphicsEnvironment.isHeadless()) throw new HeadlessException (); }/*************************************************************************//* * Instance Methods *//** * Returns the number of items in the list. * * @return The number of items in the list. */public intgetItemCount(){ return countItems ();}/*************************************************************************//** * Returns the number of items in the list. * * @return The number of items in the list. * * @deprecated This method is deprecated in favor of <code>getItemCount</code>. */public intcountItems(){ return(pItems.size());}/*************************************************************************//** * Returns the item at the specified index in the list. * * @param index The index into the list to return the item from. * * @exception ArrayIndexOutOfBoundsException If the index is invalid. */public StringgetItem(int index){ return((String)pItems.elementAt(index));}/*************************************************************************//** * Adds the specified item to this choice box. * * @param item The item to add. * * @exception NullPointerException If the item's value is null * * @since 1.1 */public synchronized voidadd(String item){ if (item == null) throw new NullPointerException ("item must be non-null"); pItems.addElement(item); int i = pItems.size () - 1; if (peer != null) { ChoicePeer cp = (ChoicePeer) peer; cp.add (item, i); } else if (selectedIndex == -1) select(0);}/*************************************************************************//** * Adds the specified item to this choice box. * * This method is oboslete since Java 2 platform 1.1. Please use @see add * instead. * * @param item The item to add. * * @exception NullPointerException If the item's value is equal to null */public synchronized voidaddItem(String item){ add(item);}/*************************************************************************//** Inserts an item into this Choice. Existing items are shifted * upwards. If the new item is the only item, then it is selected. * If the currently selected item is shifted, then the first item is * selected. If the currently selected item is not shifted, then it * remains selected. * * @param item The item to add. * @param index The index at which the item should be inserted. * * @exception IllegalArgumentException If index is less than 0 */public synchronized void
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -