简体   繁体   English

制作JFrame和Observable对象

[英]Making a JFrame and Observable Object

I have a class let's say MyJFrame which represent the GUI of my application. 我有一个类让我们说MyJFrame代表我的应用程序的GUI。 It implements the interface Observer and override the method update . 它实现了Observer接口并覆盖了方法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 . 现在我想让我的JFram成为一个Observable对象,但我不能,因为它已经扩展了类JFrame I tried to create a variable of type Observable in my class. 我试图在我的类中创建一个Observable类型的变量。

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. 这里的问题是我可以将Observer添加到这个可观察字段,我也可以通知观察者,但我不能调用方法setChanghed() (因为它被声明为protected),必须在通知之前调用它。

Do you have any idea about I can implement it? 你对我能实现它有什么想法吗?

Thanks!! 谢谢!!

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. 我认为很多,我自己包括使用Observer设计模式而不使用Java的正式Observer/Observable接口/类,并且有很多方法可以实现它,尤其是在Swing中。 Probably the most flexible is to use PropertyChangeSupport with PropertyChangeListeners which is how SwingWorkers implement this. 可能最灵活的是使用PropertyChangeSupportPropertyChangeListeners ,这是SwingWorkers实现它的方式。 Another is to use the very basic ChangeListener interface. 另一种是使用非常基本的ChangeListener接口。

If you need more specific help, please provide more detail on your problem and likely we can offer suggestions. 如果您需要更具体的帮助,请提供有关您的问题的更多详细信息,我们可能会提供建议。

Edit 1 编辑1
This also alights on a separate but important issue that's suggested by the description of your program: your GUI class extending JFrame. 这也解决了程序描述所暗示的一个单独但重要的问题:扩展JFrame的GUI类。 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. 许多人(包括我自己)建议你应该避免这样做,除非你的代码改变了JFrame的内在行为 - 那就是你的类重写了一个或多个JFrame的方法。 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 编辑2
Next unrequested recommendation: I try to gear my GUI code toward creating JPanels rather than JFrames. 下一个未经请求的建议:我尝试将我的GUI代码设置为创建JPanel而不是JFrame。 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. 这样,如果我想将GUI显示为独立的GUI,我只需动态创建一个JFrame,将我的gui的JPanel放入JFrame的contentPane,打包JFrame并显示它。 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,... . 如果我想将gui放入JDialog,JApplet,JOptionPane,或者使用容器,另一个JPanel,...的CardLayout中,我会执行相同的过程。 This gives me tremendous flexibility in how I use my gui's. 这为我如何使用我的gui提供了极大的灵活性。

扩展Observable并声明setChanged() public。

This will do it. 这样做。

public class MyJFrame extends JFrame implements Observer {
    private static class MyObservable extends Observable {
        @Override
        public void setChanged() {
            super.setChanged();
        }
    }
    private final MyObservable observable = new MyObservable();

    // rest of your code
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM