当前位置: 技术问答>java相关
怎么编写自己的listener呢?
来源: 互联网 发布时间:2015-03-28
本文导语: 当一个变量改变的时候 某个过程自己就启动 就象mouseAdapter一样 | 自己定义一个Listener 类extends EventListener, 在implement这个listener的类中 定义add,remove,fire这个事件的方法 在变量改...
当一个变量改变的时候 某个过程自己就启动
就象mouseAdapter一样
就象mouseAdapter一样
|
自己定义一个Listener 类extends EventListener,
在implement这个listener的类中
定义add,remove,fire这个事件的方法
在变量改变时调用fire方法
在implement这个listener的类中
定义add,remove,fire这个事件的方法
在变量改变时调用fire方法
|
There is already an "Observer Patterns" for this purpose.
http://developer.java.sun.com/developer/technicalArticles/Programming/KeepObjectsInSync/
import java.awt.*;
import java.applet.Applet;
import java.util.Observer;
import java.util.Observable;
/** This class extends the Observable class by making
two methods public. This is necessary in order to
delegate observable behavior in the counter class */
class MyObservable extends Observable {
public void clearChanged() {
super.clearChanged();
}
public void setChanged() {
super.setChanged();
}
}
/** This is a counter class that delegates observable behavior
to a containted MyObservable object. A method is provided
to access this obect. The counter object has a private integer
variable to hold the counter value, a text field to display it,
and a button to increment the value. Note that since this Counter
is a GUI widget, it inherits it's behavor from the AWT class Panel.
Since we also want it to be observable, that behavior must be provided
through delegation. */
class Counter extends Panel {
Label countField;
Button incButton;
private int _count = 0;
MyObservable _observable;
public Counter() {
setLayout(new BorderLayout());
add("Center",countField = new Label("Count: 0"));
add("South",incButton = new Button("Increment"));
_observable = new MyObservable();
}
public int value() {
return _count;
}
public Observable observable() {
return _observable;
}
public boolean action(Event evt, Object arg) {
if(evt.target == incButton) {
_count++;
countField.setText("Count: "+ _count);
_observable.setChanged();
_observable.notifyObservers(this);
return true;
}
return false;
}
}
/** This class provides a textual view of the counter,
and is an observer of the counter */
class TextView extends TextField implements Observer {
public TextView(Counter c) {
super(10);
setEditable(false);
setText(String.valueOf(c.value()));
}
public void update(Observable o, Object counter) {
setText(String.valueOf(((Counter)counter).value()));
}
}
/** This class provides a scroll bar view of the counter value */
class ScrollView extends Scrollbar implements Observer {
public ScrollView(Counter c) {
super(Scrollbar.HORIZONTAL,0,1,0,10);
setValue(c.value());
}
public void update(Observable o, Object counter) {
setValue(((Counter)counter).value());
}
}
/** A simple applet to show the Counter and its two
Observers, a TextView and a ScrollView */
public class ObserverTest extends Applet {
Counter c;
TextView tv;
ScrollView sv;
public ObserverTest() {
setLayout(new BorderLayout());
add("North",c = new Counter());
add("Center",tv = new TextView(c));
add("South",sv = new ScrollView(c));
c.observable().addObserver(tv);
c.observable().addObserver(sv);
}
}
http://developer.java.sun.com/developer/technicalArticles/Programming/KeepObjectsInSync/
import java.awt.*;
import java.applet.Applet;
import java.util.Observer;
import java.util.Observable;
/** This class extends the Observable class by making
two methods public. This is necessary in order to
delegate observable behavior in the counter class */
class MyObservable extends Observable {
public void clearChanged() {
super.clearChanged();
}
public void setChanged() {
super.setChanged();
}
}
/** This is a counter class that delegates observable behavior
to a containted MyObservable object. A method is provided
to access this obect. The counter object has a private integer
variable to hold the counter value, a text field to display it,
and a button to increment the value. Note that since this Counter
is a GUI widget, it inherits it's behavor from the AWT class Panel.
Since we also want it to be observable, that behavior must be provided
through delegation. */
class Counter extends Panel {
Label countField;
Button incButton;
private int _count = 0;
MyObservable _observable;
public Counter() {
setLayout(new BorderLayout());
add("Center",countField = new Label("Count: 0"));
add("South",incButton = new Button("Increment"));
_observable = new MyObservable();
}
public int value() {
return _count;
}
public Observable observable() {
return _observable;
}
public boolean action(Event evt, Object arg) {
if(evt.target == incButton) {
_count++;
countField.setText("Count: "+ _count);
_observable.setChanged();
_observable.notifyObservers(this);
return true;
}
return false;
}
}
/** This class provides a textual view of the counter,
and is an observer of the counter */
class TextView extends TextField implements Observer {
public TextView(Counter c) {
super(10);
setEditable(false);
setText(String.valueOf(c.value()));
}
public void update(Observable o, Object counter) {
setText(String.valueOf(((Counter)counter).value()));
}
}
/** This class provides a scroll bar view of the counter value */
class ScrollView extends Scrollbar implements Observer {
public ScrollView(Counter c) {
super(Scrollbar.HORIZONTAL,0,1,0,10);
setValue(c.value());
}
public void update(Observable o, Object counter) {
setValue(((Counter)counter).value());
}
}
/** A simple applet to show the Counter and its two
Observers, a TextView and a ScrollView */
public class ObserverTest extends Applet {
Counter c;
TextView tv;
ScrollView sv;
public ObserverTest() {
setLayout(new BorderLayout());
add("North",c = new Counter());
add("Center",tv = new TextView(c));
add("South",sv = new ScrollView(c));
c.observable().addObserver(tv);
c.observable().addObserver(sv);
}
}
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。