| 
 | JavaTM 2 Platform Standard Ed. 6 | |||||||||
| 上一個類別 下一個類別 | 框架 無框架 | |||||||||
| 摘要: 巢狀 | 欄位 | 建構子 | 方法 | 詳細資訊: 欄位 | 建構子 | 方法 | |||||||||
java.lang.Objectjava.awt.Component
java.awt.List
public class List
List 元件為使用者提供了一個可滾動的文本項列表。可設置此 list,使其允許使用者進行單項或多項選擇。 
 
例如以下程式碼:
 List lst = new List(4, false);
 lst.add("Mercury");
 lst.add("Venus");
 lst.add("Earth");
 lst.add("JavaSoft");
 lst.add("Mars");
 lst.add("Jupiter");
 lst.add("Saturn");
 lst.add("Uranus");
 lst.add("Neptune");
 lst.add("Pluto");
 cnt.add(lst);
 
當 cnt 為容器時,將產生以下滾動列表:
 
 
 
如果 List 允許進行多項選擇,則單擊已選中的項時,將取消選中該項。在上面的範例中,一次只能從滾動列表中選擇一項,因為創建新的滾動列表時,第二個參數為 false。如果 List 不允許進行多項選擇,則選擇某一項會導致其他選中的項取消選中。
 
注意,本範例中顯示的列表是用四個可視行創建的。創建該列表之後,不可更改可視行的數量。預設的 List 是用四行創建的,所以 lst = new List() 與 list = new List(4, false) 等效。
 
從 Java 1.1 開始,AWT(Abstract Window Toolkit,抽象視窗工具套件)會把列表上發生的所有鼠標、鍵盤和焦點事件發送給 List 物件。(維護舊的 AWT 事件模型的目的是為了向後相容,不推薦使用它。)
 
當使用者選中或取消選中某項時,AWT 將向列表發送一個 ItemEvent 實例。當使用者雙擊滾動列表中的某一項時,AWT 會在緊隨項事件後向列表發送一個 ActionEvent 實例。當使用者選中列表中的某項,按下 return 鍵時,AWT 也會產生一個動作事件。
 
如果應用程序需要基於此列表中使用者選中或啟動的項執行某個動作,則應該相應地實作 ItemListener 或 ActionListener,並註冊新的偵聽器,以便在此列表中接收事件。
 
對於多項選擇滾動列表,使用外部動作(如單擊按鈕)來觸發動作被認為是一種更好的使用者介面。
ItemEvent, 
ItemListener, 
ActionEvent, 
ActionListener, 
序列化表格| 巢狀類別摘要 | |
|---|---|
| protected  class | List.AccessibleAWTList此類別實作 List類別的可存取性支持。 | 
| 從類別 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 | 
| 建構子摘要 | |
|---|---|
| List()創建新的滾動列表。 | |
| List(int rows)創建一個用指定可視行數初始化的新滾動列表。 | |
| List(int rows,
     boolean multipleMode)創建一個初始化為顯示指定行數的新滾動列表。 | |
| 方法摘要 | ||
|---|---|---|
|  void | add(String item)向滾動列表的末尾添加指定的項。 | |
|  void | add(String item,
    int index)向滾動列表中索引指示的位置添加指定的項。 | |
|  void | addActionListener(ActionListener l)添加指定的動作偵聽器以從此列表接收動作事件。 | |
|  void | addItem(String item)已過時。 由 add(String)取代。 | |
|  void | addItem(String item,
        int index)已過時。 由 add(String, int)取代。 | |
|  void | addItemListener(ItemListener l)添加指定的項偵聽器以接收此列表的項事件。 | |
|  void | addNotify()創建列表的同位體。 | |
|  boolean | allowsMultipleSelections()已過時。 從 JDK version 1.1 開始,由 isMultipleMode()取代。 | |
|  void | clear()已過時。 從 JDK version 1.1 開始,由 removeAll()取代。 | |
|  int | countItems()已過時。 從 JDK version 1.1 開始,由 getItemCount()取代。 | |
|  void | delItem(int position)已過時。 由 remove(String)和remove(int)取代。 | |
|  void | delItems(int start,
         int end)已過時。 從 JDK version 1.1 開始,後來不再公開使用。只希望作為包私有方法時保留此方法。 | |
|  void | deselect(int index)取消選擇指定索引處的項。 | |
|  AccessibleContext | getAccessibleContext()獲取與此 List關聯的AccessibleContext。 | |
|  ActionListener[] | getActionListeners()返回已在此列表上註冊的所有動作偵聽器的陣列。 | |
|  String | getItem(int index)獲取與指定索引關聯的項。 | |
|  int | getItemCount()獲取列表中的項數。 | |
|  ItemListener[] | getItemListeners()返回已在此列表上註冊的所有項偵聽器的陣列。 | |
|  String[] | getItems()獲取列表中的項。 | |
| 
 | getListeners(Class<T> listenerType)返回目前已在此 List上註冊為FooListener的所有物件的陣列。 | |
|  Dimension | getMinimumSize()確定此滾動列表的最小大小。 | |
|  Dimension | getMinimumSize(int rows)獲取具有指定行數的列表的最少維數。 | |
|  Dimension | getPreferredSize()獲取此滾動列表的首選大小。 | |
|  Dimension | getPreferredSize(int rows)獲取具有指定行數的列表的首選維數。 | |
|  int | getRows()獲取此列表中的可視行數。 | |
|  int | getSelectedIndex()獲取列表中選中項的索引。 | |
|  int[] | getSelectedIndexes()獲取列表中選中的索引。 | |
|  String | getSelectedItem()獲取此滾動列表中選中的項。 | |
|  String[] | getSelectedItems()獲取此滾動列表中選中的項。 | |
|  Object[] | getSelectedObjects()獲取物件陣列中此滾動列表的選中項。 | |
|  int | getVisibleIndex()獲取上次由 makeVisible方法使其可視的項的索引。 | |
|  boolean | isIndexSelected(int index)確定是否已選中此滾動列表中的指定項。 | |
|  boolean | isMultipleMode()確定此列表是否允許進行多項選擇。 | |
|  boolean | isSelected(int index)已過時。 從 JDK version 1.1 開始,由 isIndexSelected(int)取代。 | |
|  void | makeVisible(int index)使指定索引處的項可視。 | |
|  Dimension | minimumSize()已過時。 從 JDK version 1.1 開始,由 getMinimumSize()取代。 | |
|  Dimension | minimumSize(int rows)已過時。 從 JDK version 1.1 開始,由 getMinimumSize(int)取代。 | |
| protected  String | paramString()返回表示此滾動列表狀態的參數字元串。 | |
|  Dimension | preferredSize()已過時。 從 JDK version 1.1 開始,由 getPreferredSize()取代。 | |
|  Dimension | preferredSize(int rows)已過時。 從 JDK version 1.1 開始,由 getPreferredSize(int)取代。 | |
| protected  void | processActionEvent(ActionEvent e)處理發生在此列表上的動作事件,方法是將這些事件指派給所有已註冊的 ActionListener物件。 | |
| protected  void | processEvent(AWTEvent e)此滾動列表的進程事件。 | |
| protected  void | processItemEvent(ItemEvent e)處理發生在此列表上的項事件,方法是將這些事件指派給所有已註冊的 ItemListener物件。 | |
|  void | remove(int position)從此滾動列表中移除指定位置處的項。 | |
|  void | remove(String item)從列表中移除項的第一次出現。 | |
|  void | removeActionListener(ActionListener l)移除指定的動作偵聽器,以便不再從此列表接收動作事件。 | |
|  void | removeAll()從此列表中移除所有項。 | |
|  void | removeItemListener(ItemListener l)移除指定的項偵聽器,以便不再從此列表接收項事件。 | |
|  void | removeNotify()移除此列表的同位體。 | |
|  void | replaceItem(String newValue,
            int index)使用新字元串替換滾動列表中指定索引處的項。 | |
|  void | select(int index)選擇滾動列表中指定索引處的項。 | |
|  void | setMultipleMode(boolean b)設置確定此列表是否允許進行多項選擇的標誌。 | |
|  void | setMultipleSelections(boolean b)已過時。 從 JDK version 1.1 開始,由 setMultipleMode(boolean)取代。 | |
| 從類別 java.lang.Object 繼承的方法 | 
|---|
| clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait | 
| 建構子詳細資訊 | 
|---|
public List()
     throws HeadlessException
List(0, false) 的一種便捷方法。還要注意,列表中的可視行數一旦創建就不能更改。
HeadlessException - 如果為 GraphicsEnvironment.isHeadless(),則返回 true。GraphicsEnvironment.isHeadless()
public List(int rows)
     throws HeadlessException
List(rows, false) 的一種便捷方法。還要注意,列表中的可視行數一旦創建就不能更改。
rows - 要顯示的項數。
HeadlessException - 如果為 GraphicsEnvironment.isHeadless(),則返回 true。GraphicsEnvironment.isHeadless()
public List(int rows,
            boolean multipleMode)
     throws HeadlessException
multipleMode 的值為 true,則使用者可從列表中選擇多項。如果為 false,則一次只能選擇一項。
rows - 要顯示的項數。multipleMode - 如果為 true,則允許進行多項選擇;否則,一次只能選擇一項。
HeadlessException - 如果為 GraphicsEnvironment.isHeadless(),則返回 true。GraphicsEnvironment.isHeadless()| 方法詳細資訊 | 
|---|
public void addNotify()
Component 中的 addNotifyComponent.isDisplayable(), 
Component.removeNotify()public void removeNotify()
Component 中的 removeNotifyComponent.isDisplayable(), 
Component.addNotify()public int getItemCount()
getItem(int)@Deprecated public int countItems()
getItemCount() 取代。
public String getItem(int index)
index - 項的位置
getItemCount()public String[] getItems()
select(int), 
deselect(int), 
isIndexSelected(int)public void add(String item)
item - 要添加的項@Deprecated public void addItem(String item)
add(String) 取代。
public void add(String item,
                int index)
item - 要添加的項;如果此參數為 null,則該項被視為空字元串 ""index - 添加項的位置
@Deprecated
public void addItem(String item,
                               int index)
add(String, int) 取代。
public void replaceItem(String newValue,
                        int index)
newValue - 一個替換現有項的新字元串index - 要替換項的位置
ArrayIndexOutOfBoundsException - 如果 index 超出範圍。public void removeAll()
remove(java.lang.String), 
delItems(int, int)@Deprecated public void clear()
removeAll() 取代。
public void remove(String item)
item - 從列表中移除的項
IllegalArgumentException - 如果列表中不存在該項public void remove(int position)
position - 要刪除項的索引
ArrayIndexOutOfBoundsException - 如果 position 小於零或大於 getItemCount()-1add(String, int)@Deprecated public void delItem(int position)
remove(String) 和 remove(int) 取代。
public int getSelectedIndex()
-1。select(int), 
deselect(int), 
isIndexSelected(int)public int[] getSelectedIndexes()
select(int), 
deselect(int), 
isIndexSelected(int)public String getSelectedItem()
null。select(int), 
deselect(int), 
isIndexSelected(int)public String[] getSelectedItems()
select(int), 
deselect(int), 
isIndexSelected(int)public Object[] getSelectedObjects()
ItemSelectable 中的 getSelectedObjectsObject 陣列;如果沒有選中的項,則會返回一個零長度的陣列。getSelectedItems(), 
ItemSelectablepublic void select(int index)
注意,超出參數範圍是無效的,並且將導致未指定的行為。
注意,此方法應主要用於初始選擇此元件中的項。以程式方式調用此方法不 會觸發 ItemEvent。觸發 ItemEvent 的唯一方式是通過使用者交互。
index - 要選擇的項位置getSelectedItem(), 
deselect(int), 
isIndexSelected(int)public void deselect(int index)
注意,超出參數範圍是無效的,並且將導致未指定的行為。
如果指定索引處的項未選中,則忽略操作。
index - 要取消選擇項的位置select(int), 
getSelectedItem(), 
isIndexSelected(int)public boolean isIndexSelected(int index)
index - 要檢查的項
true;否則返回 falseselect(int), 
deselect(int)@Deprecated public boolean isSelected(int index)
isIndexSelected(int) 取代。
public int getRows()
List 創建,行數將永遠不變。
public boolean isMultipleMode()
true;否則返回 falsesetMultipleMode(boolean)@Deprecated public boolean allowsMultipleSelections()
isMultipleMode() 取代。
public void setMultipleMode(boolean b)
b - 如果為 true,則允許進行多項選擇;否則,只能一次從列表中選擇一項。isMultipleMode()@Deprecated public void setMultipleSelections(boolean b)
setMultipleMode(boolean) 取代。
public int getVisibleIndex()
makeVisible 方法使其可視的項的索引。
makeVisible(int)public void makeVisible(int index)
index - 項的位置getVisibleIndex()public Dimension getPreferredSize(int rows)
rows - 列表的行數
Component.getPreferredSize()@Deprecated public Dimension preferredSize(int rows)
getPreferredSize(int) 取代。
public Dimension getPreferredSize()
Component 中的 getPreferredSizeComponent.getPreferredSize()@Deprecated public Dimension preferredSize()
getPreferredSize() 取代。
Component 中的 preferredSizepublic Dimension getMinimumSize(int rows)
rows - 列表的行數
Component.getMinimumSize()@Deprecated public Dimension minimumSize(int rows)
getMinimumSize(int) 取代。
public Dimension getMinimumSize()
Component 中的 getMinimumSizeComponent.getMinimumSize()@Deprecated public Dimension minimumSize()
getMinimumSize() 取代。
Component 中的 minimumSizepublic void addItemListener(ItemListener l)
select 或 deselect 的調用。如果偵聽器 l 為 null,則不會拋出異常,並且不執行動作。
有關 AWT 的執行緒模型的詳細資訊,請參閱 AWT 執行緒問題。
ItemSelectable 中的 addItemListenerl - 項偵聽器removeItemListener(java.awt.event.ItemListener), 
getItemListeners(), 
select(int), 
deselect(int), 
ItemEvent, 
ItemListenerpublic void removeItemListener(ItemListener l)
l 為 null,則不會拋出異常,並且不執行動作。
有關 AWT 的執行緒模型的詳細資訊,請參閱 AWT 執行緒問題。
ItemSelectable 中的 removeItemListenerl - 項偵聽器addItemListener(java.awt.event.ItemListener), 
getItemListeners(), 
ItemEvent, 
ItemListenerpublic ItemListener[] getItemListeners()
ItemListener,如果當前沒有已註冊的項偵聽器,則返回一個空陣列。addItemListener(java.awt.event.ItemListener), 
removeItemListener(java.awt.event.ItemListener), 
ItemEvent, 
ItemListenerpublic void addActionListener(ActionListener l)
如果偵聽器 l 為 null,則不會拋出異常,並且不執行動作。
有關 AWT 的執行緒模型的詳細資訊,請參閱 AWT 執行緒問題。
l - 動作偵聽器removeActionListener(java.awt.event.ActionListener), 
getActionListeners(), 
ActionEvent, 
ActionListenerpublic void removeActionListener(ActionListener l)
l 為 null,則不會拋出異常,並且不執行動作。
有關 AWT 的執行緒模型的詳細資訊,請參閱 AWT 執行緒問題。
l - 動作偵聽器addActionListener(java.awt.event.ActionListener), 
getActionListeners(), 
ActionEvent, 
ActionListenerpublic ActionListener[] getActionListeners()
ActionListener,如果當前沒有已註冊的動作偵聽器,則返回一個空陣列。addActionListener(java.awt.event.ActionListener), 
removeActionListener(java.awt.event.ActionListener), 
ActionEvent, 
ActionListenerpublic <T extends EventListener> T[] getListeners(Class<T> listenerType)
List 上註冊為 FooListener 的所有物件的陣列。FooListener 是用 addFooListener 方法註冊的。
 
可以使用 class 文字值來指定 listenerType 參數,如 FooListener.class。例如,可以查詢其項偵聽器具有以下程式碼的 List l:
 
ItemListener[] ils = (ItemListener[])(l.getListeners(ItemListener.class));如果不存在這樣的偵聽器,則此方法將返回一個空陣列。
Component 中的 getListenerslistenerType - 請求的偵聽器型別;此參數應該指定一個從 java.util.EventListener 遺傳下來的介面
FooListener 的所有物件的陣列
ClassCastException - 如果 listenerType 未指定實作 java.util.EventListener 的類別或介面。getItemListeners()protected void processEvent(AWTEvent e)
ItemEvent 的一個實例,則該事件將調用 processItemEvent 方法。另外,如果事件是 ActionEvent 的一個實例,則它將調用 processActionEvent。如果事件不是一個項事件或動作事件,則它將調用父級類別的 processEvent。
注意,如果事件參數為 null,則行為未指定,並可能拋出異常。
Component 中的 processEvente - 事件ActionEvent, 
ItemEvent, 
processActionEvent(java.awt.event.ActionEvent), 
processItemEvent(java.awt.event.ItemEvent)protected void processItemEvent(ItemEvent e)
ItemListener 物件。
 除非此元件啟用了項事件,才會調用此方法。當出現以下情況時,將啟用項事件:
ItemListener 物件通過 addItemListener 註冊。
enableEvents 啟用。
注意,如果事件參數為 null,則行為未指定,並可能拋出異常。
e - 項事件ItemEvent, 
ItemListener, 
addItemListener(java.awt.event.ItemListener), 
Component.enableEvents(long)protected void processActionEvent(ActionEvent e)
ActionListener 物件。
 除非此元件啟用了動作事件,才會調用此方法。當出現以下情況時,將啟用動作事件:
ActionListener 物件通過 addActionListener 註冊。
enableEvents 啟用。
 注意,如果事件參數為 null,則行為未指定,並可能導致一個異常。
e - 動作事件ActionEvent, 
ActionListener, 
addActionListener(java.awt.event.ActionListener), 
Component.enableEvents(long)protected String paramString()
Component 中的 paramString
@Deprecated
public void delItems(int start,
                                int end)
public AccessibleContext getAccessibleContext()
List 關聯的 AccessibleContext。對於列表,AccessibleContext 採用 AccessibleAWTList 的形式。如有必要,可以創建一個新的 AccessibleAWTList 實例。
Accessible 中的 getAccessibleContextComponent 中的 getAccessibleContextAccessibleAWTList,它將充當此 List 的 AccessibleContext。| 
 | JavaTM 2 Platform Standard Ed. 6 | |||||||||
| 上一個類別 下一個類別 | 框架 無框架 | |||||||||
| 摘要: 巢狀 | 欄位 | 建構子 | 方法 | 詳細資訊: 欄位 | 建構子 | 方法 | |||||||||
版權所有 2008 Sun Microsystems, Inc. 保留所有權利。請遵守GNU General Public License, version 2 only。