|
JavaTM 2 Platform Standard Ed. 6 |
|||||||||
| 上一個類別 下一個類別 | 框架 無框架 | |||||||||
| 摘要: 巢狀 | 欄位 | 建構子 | 方法 | 詳細資訊: 欄位 | 建構子 | 方法 | |||||||||
java.lang.Objectjavax.swing.event.EventListenerList
public class EventListenerList
保存 EventListener 列表的類別。單個實例可用來保存使用列表的實例的所有偵聽器(全部類別型)。該類別的責任是使用 EventListenerList 提供型別安全的 API(最好遵守 JavaBeans 規範)和方法,這些方法將事件通知方法指派給列表上適當的 Event Listener。 此類別的主要優點是,在沒有偵聽器的情況下,它相對廉價一些,它在單個地方為事件偵聽器提供序列化,同時還提供某種程度的 MT 安全(在正確使用時)。 用例:假定正在定義發送 FooEvent 的類別,並希望允許該類別的使用者註冊 FooListener 並在發生 FooEvent 時接收通知。則應該將以下程式碼添加到類別定義中:
EventListenerList listenerList = new EventListenerList();
FooEvent fooEvent = null;
public void addFooListener(FooListener l) {
listenerList.add(FooListener.class, l);
}
public void removeFooListener(FooListener l) {
listenerList.remove(FooListener.class, l);
}
// Notify all listeners that have registered interest for
// notification on this event type. The event instance
// is lazily created using the parameters passed into
// the fire method.
protected void fireFooXXX() {
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length-2; i>=0; i-=2) {
if (listeners[i]==FooListener.class) {
// Lazily create the event:
if (fooEvent == null)
fooEvent = new FooEvent(this);
((FooListener)listeners[i+1]).fooXXX(fooEvent);
}
}
}
應該將 foo 更改為適當的名稱,並將 fireFooXxx 也更改為適當的方法名稱。FooListener 介面中的每個通知方法都應該有一個 fire 方法。
警告:此類別的序列化物件將與以後的 Swing 版本不相容。當前的序列化支持適用於短期存儲或運行相同 Swing 版本的應用程序之間的 RMI。從 1.4 版本開始,已在 java.beans 套件中添加了支持所有 JavaBeansTM 長期存儲的功能。請參見 XMLEncoder。
| 欄位摘要 | |
|---|---|
protected Object[] |
listenerList
|
| 建構子摘要 | |
|---|---|
EventListenerList()
|
|
| 方法摘要 | ||
|---|---|---|
|
add(Class<T> t,
T l)
將偵聽器作為指定型別的偵聽器進行添加。 |
|
int |
getListenerCount()
返回此偵聽器列表中偵聽器的總數。 |
|
int |
getListenerCount(Class<?> t)
返回此偵聽器列表中所提供型別的偵聽器的總數。 |
|
Object[] |
getListenerList()
將事件偵聽器列表以 ListenerType 偵聽器對陣列的形式傳回。 |
|
|
getListeners(Class<T> t)
返回給定型別的所有偵聽器組成的陣列。 |
|
|
remove(Class<T> t,
T l)
將偵聽器作為指定型別的偵聽器進行移除。 |
|
String |
toString()
返回此 EventListenerList 的字元串表示形式。 |
|
| 從類別 java.lang.Object 繼承的方法 |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| 欄位詳細資訊 |
|---|
protected transient Object[] listenerList
| 建構子詳細資訊 |
|---|
public EventListenerList()
| 方法詳細資訊 |
|---|
public Object[] getListenerList()
public <T extends EventListener> T[] getListeners(Class<T> t)
ClassCastException - 如果所提供的類別不能分派給 EventListenerpublic int getListenerCount()
public int getListenerCount(Class<?> t)
public <T extends EventListener> void add(Class<T> t,
T l)
t - 要添加的偵聽器的型別l - 要添加的偵聽器
public <T extends EventListener> void remove(Class<T> t,
T l)
t - 要移除的偵聽器的型別l - 要移除的偵聽器public String toString()
Object 中的 toString
|
JavaTM 2 Platform Standard Ed. 6 |
|||||||||
| 上一個類別 下一個類別 | 框架 無框架 | |||||||||
| 摘要: 巢狀 | 欄位 | 建構子 | 方法 | 詳細資訊: 欄位 | 建構子 | 方法 | |||||||||
版權所有 2008 Sun Microsystems, Inc. 保留所有權利。請遵守GNU General Public License, version 2 only。