java怎样监听一个值是否发生了变化,具体代码
1个回答
2017-07-03 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
java 自定义监听器监听属性变化
import java.util.EventObject;
public class MyEvent extends EventObject
{
private Object obj;
private String sName;
public MyEvent(Object source,String sName)
{
super(source);
this.obj=source;
this.sName=sName;
}
public Object getObj()
{
return obj;
}
public String getsName()
{
return sName;
}
}
import java.util.EventListener;
public interface MyEventListener extends EventListener
{
public void handleEvent (MyEvent me);
}
import java.util.Iterator;
import java.util.Vector;
import demo.DemoEvent;
public class MyEventSource
{
private Vector list=new Vector();
private String sName = "";
public MyEventSource()
{
super();
}
public void addMyEventListener(MyEventListener me)
{
list.add(me);
}
public void deleteMyEventListener(MyEventListener me)
{
list.remove(me);
}
public void notifyMyEvent(MyEvent me)
{
Iterator it=list.iterator();
while(it.hasNext())
{
((MyEventListener) it.next()).handleEvent(me);
}
}
public void setName(String str)
{
boolean bool = false;
if (str == null && sName != null)
bool = true;
else if (str != null && sName == null)
bool = true;
else if (!sName.equals(str))
bool = true;
this.sName = str;
// 如果改变则执行事件
if (bool)
notifyMyEvent(new MyEvent(this, sName));
}
public String getsName()
{
return sName;
}
}
public class Test implements MyEventListener
{
public Test()
{
MyEventSource mes = new MyEventSource();
mes.addMyEventListener(this);
mes.setName("niu");
}
public static void main(String args[])
{
new Test();
}
public void handleEvent(MyEvent me)
{
System.out.println(me.getSource());
System.out.println(me.getsName());
}
}
import java.util.EventObject;
public class MyEvent extends EventObject
{
private Object obj;
private String sName;
public MyEvent(Object source,String sName)
{
super(source);
this.obj=source;
this.sName=sName;
}
public Object getObj()
{
return obj;
}
public String getsName()
{
return sName;
}
}
import java.util.EventListener;
public interface MyEventListener extends EventListener
{
public void handleEvent (MyEvent me);
}
import java.util.Iterator;
import java.util.Vector;
import demo.DemoEvent;
public class MyEventSource
{
private Vector list=new Vector();
private String sName = "";
public MyEventSource()
{
super();
}
public void addMyEventListener(MyEventListener me)
{
list.add(me);
}
public void deleteMyEventListener(MyEventListener me)
{
list.remove(me);
}
public void notifyMyEvent(MyEvent me)
{
Iterator it=list.iterator();
while(it.hasNext())
{
((MyEventListener) it.next()).handleEvent(me);
}
}
public void setName(String str)
{
boolean bool = false;
if (str == null && sName != null)
bool = true;
else if (str != null && sName == null)
bool = true;
else if (!sName.equals(str))
bool = true;
this.sName = str;
// 如果改变则执行事件
if (bool)
notifyMyEvent(new MyEvent(this, sName));
}
public String getsName()
{
return sName;
}
}
public class Test implements MyEventListener
{
public Test()
{
MyEventSource mes = new MyEventSource();
mes.addMyEventListener(this);
mes.setName("niu");
}
public static void main(String args[])
{
new Test();
}
public void handleEvent(MyEvent me)
{
System.out.println(me.getSource());
System.out.println(me.getsName());
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询