JAVA中,多个对象(我这里是几个JFrame)间共享一个变量(封装在一个我自己定义的类中,里面只是需要共享的变量).
如果一个JFrame改变了这个变量的值,能否在其它JFrame里触发一个事件?
就像actionListener,MouseMotionListener之类的,有没有一个类似的变量监听器"VariableListener"?
谢谢~
如果一个JFrame改变了这个变量的值,能否在其它JFrame里触发一个事件?
就像actionListener,MouseMotionListener之类的,有没有一个类似的变量监听器"VariableListener"?
谢谢~
{
public static void main(String[] args)
{
C1 ce=new C1();
ce.i=1;
Cobserver cr=new Cobserver();
ce.addObserver(cr);
ce.i=2;
ce.notifyObservers(); }
static class C1 extends Observable
{
int i=0;
}
static class Cobserver implements Observer
{
public void update(Observable o, Object arg)
{
System.out.println("updated");
}
}
}是这样吗?总是没有输出.请问要怎么改?
ce.setChanged();
The method setChanged() from the type Observable is not visible
或者在class C1中重写setChanged()方法,改为public的
类似这样
public void setChanged(){
super.setChanged();
}