制作 JFrame 和可观察对象
我有一个类 MyJFrame
,它代表我的应用程序的 GUI。它实现了接口Observer
并重写了方法update
。
public class MyJFrame extends JFrame implements Observer{
...
public void update(Observable arg0, Object arg1){
...
}
}
现在我也想让我的 JFram 成为一个 Observable 对象,但我不能,因为它已经扩展了类 JFrame
。我尝试在我的类中创建一个 Observable 类型的变量。
public class MyJFrame extends JFrame implements Observer{
Observable observable = new Observable();
这里的问题是,我可以将观察者添加到这个可观察字段,也可以通知观察者,但我无法调用必须在之前调用的方法 setChanged()
(因为它被声明为 protected)通知。
您对我可以实施它有什么想法吗?
谢谢!!
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){
...
}
}
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();
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.
Do you have any idea about I can implement it?
Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想这里的很多人,包括我自己,都使用了观察者设计模式,而不使用Java的正式
观察者/Observable
接口/类,并且有很多方法可以实现它,特别是在 Swing 中。最灵活的可能是使用PropertyChangeSupport
和PropertyChangeListeners
,这就是 SwingWorkers 实现这一点的方式。另一种是使用非常基本的 ChangeListener 接口。如果您需要更具体的帮助,请提供有关您问题的更多详细信息,我们可能会提供建议。
编辑 1
这也引起了程序描述中建议的一个单独但重要的问题:扩展 JFrame 的 GUI 类。这里的许多人(同样包括我自己)建议您应该避免这样做,除非您的代码改变了 JFrame 的内在行为——也就是说您的类重写了 JFrame 的一个或多个方法。这是关于通过继承或通过组合增强类哪个更好的更大讨论中的一个小子讨论。
例如:更喜欢组合而不是继承?
编辑 2
下一个未请求的建议:我尝试将 GUI 代码调整为创建 JPanel 而不是 JFrame。这样,如果我想将我的 GUI 显示为独立的 GUI,我只需动态创建一个 JFrame,将我的 gui 的 JPanel 放入 JFrame 的 contentPane 中,打包 JFrame 并显示它。如果我想将 gui 放入 JDialog、JApplet、JOptionPane 中,或者也放入使用容器的 CardLayout、另一个 JPanel 中,我会执行相同的过程,...。这为我如何使用 GUI 提供了极大的灵活性。
I think many here, myself included use the Observer Design Pattern without using Java's formal
Observer/Observable
interface/class, and there are many ways to implement this, especially in Swing. Probably the most flexible is to usePropertyChangeSupport
withPropertyChangeListeners
which is how SwingWorkers implement this. Another is to use the very basic ChangeListener interface.If you need more specific help, please provide more detail on your problem and likely we can offer suggestions.
Edit 1
This also alights on a separate but important issue that's suggested by the description of your program: your GUI class extending JFrame. Many here (again myself included) recommend that you should avoid doing this unless your code alters the intrinsic behavior of JFrame -- that is that your class overrides one or more of JFrame's methods. This is a small sub-discussion in the much greater discussion on which is better: enhancing classes through inheritance or through composition.
For example: Prefer composition over inheritance?
Edit 2
Next unrequested recommendation: I try to gear my GUI code toward creating JPanels rather than JFrames. This way if I want to display my GUI as a stand-alone GUI, I simply create a JFrame on the fly, place my gui's JPanel into the JFrame's contentPane, pack the JFrame and display it. I do the same procedure if I want to place the gui into a JDialog, a JApplet, a JOptionPane, or also in a CardLayout using container, another JPanel,... . This gives me tremendous flexibility in how I use my gui's.
扩展
Observable
并将setChanged()
声明为公共。Extend
Observable
and declaresetChanged()
public.这样就可以了。
This will do it.