制作JFrame和Observable对象 [英] Making a JFrame and Observable Object
问题描述
我有一个类让我们说 MyJFrame
代表我的应用程序的GUI。它实现接口 Observer
并覆盖方法 update
。
I have a class let's say MyJFrame
which represent the GUI of my application. It implements the interface Observer
and override the method update
.
public class MyJFrame extends JFrame implements Observer{
...
public void update(Observable arg0, Object arg1){
...
}
}
现在我想让我的JFram成为一个Observable对象但是我不能,因为它已经扩展了类 JFrame
。我试图在我的类中创建一个Observable类型的变量。
Now I want to make also my JFram an Observable object but I can't because it already extend the class JFrame
. I tried to create a variable of type Observable in my class.
public class MyJFrame extends JFrame implements Observer{
Observable observable = new Observable();
这里的问题是我可以将Observer添加到这个可观察字段,我也可以通知观察者但是我无法调用方法 setChanghed()
(因为它被声明为受保护),必须在通知之前调用它。
The problem here is that I can add Observer to this observable field and I can also notify the observer but I cannot invoke the method setChanghed()
(because it is declared as protected) which has to be called before the notification.
你知道我可以实现吗?
谢谢!
推荐答案
扩展 Observable
并声明 setChanged()
public。
Extend Observable
and declare setChanged()
public.
这篇关于制作JFrame和Observable对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!