简体   繁体   English

两个不同的JFrame之间的通信?

[英]Communication between two different JFrames?

Hello as maybe you have heard about GIMP or something like that which uses different frames As a complete gui so I was wondering how to do such frames communications when both(maybe multiple) frames are loaded in memory and are visible. 您好,也许您听说过GIMP或类似的使用不同帧的内容作为完整的gui,所以我想知道当两个(也许多个)帧都加载到内存中并且可见时如何进行此类帧通信。

I have gone through some articles but they were not so satisfactory, if anyone have a good example or tutorial then please share. 我浏览了一些文章,但并不令人满意,如果有人有一个很好的例子或教程,请分享。

Regards Alok sharma 关于Alok Sharma

Basically, it's just a matter of having a reference to frame A in frame B, and a reference to frame B in frame A : 基本上,只需要在帧B中引用帧A,在帧A中引用帧B即可:

public class FrameA extends JFrame {
    private FrameB frameB;

    public void setFrameB(FrameB frameB) {
        this.frameB = frameB;
    }

    public void foo() {
        // change things in this frame
        frameB.doSomethingBecauseFrameAHasChanged();
    }
}

public class FrameB extends JFrame {
    private FrameA frameA;

    public void setFrameA(FrameA frameA) {
        this.frameA = frameA;
    }

    public void bar() {
        // change things in this frame
        frameA.doSomethingBecauseFrameBHasChanged();
    }
}

public class Main {
    public static void main(String[] args) {
        FrameA frameA = new FrameA();
        FrameB frameB = new FrameB();
        frameA.setFrameB(frameB);
        frameB.setFrameA(frameA);
        // make both frames visible
    }
}

Most of the time, interfaces are introduced to decouple the frames (listeners, etc.), or a mediator is used in order to avoid too much linkings between all the frames, but you should get the idea. 大多数时候,引入接口来解耦框架(侦听器等),或者使用调解器来避免所有框架之间的过多链接,但是您应该明白这一点。

If you are separating out your "Control" logic from your "View" logic in a MVC type pattern this should be as simple as just referencing a different component. 如果要在MVC类型模式中将“控件”逻辑与“视图”逻辑分开,则这应该就像引用其他组件一样简单。

Just like a JFrame might have multiple panels and your application can make changes to several panels based on actions in a single panel. 就像JFrame可能具有多个面板一样,您的应用程序可以基于单个面板中的操作对多个面板进行更改。 Your application can have multiple frames that can be affected by actions in a single frame. 您的应用程序可以具有多个框架,这些框架可能会受到单个框架中操作的影响。

Consider to build your application on top of the NetBeans Platform (a Swing-bsed RCP), which comes with a window system. 考虑在基于Windows的NetBeans平台(Swing平台的RCP)上构建应用程序。 You can have the TopComponets detached, if you prefer multiple windows. 如果您喜欢多个窗口,则可以分离TopComponets。

You can communicate between TopComponents and Modules via a Lookup instance. 您可以通过Lookup实例在TopComponents和Modules之间进行通信。

I'm 7 years late, but maybe this is still interesting for you ;) I solved this issue by implementing the Observer-Pattern. 我已经晚了7年,但也许这对您来说仍然很有趣;)我通过实现Observer-Pattern解决了这个问题。

In my example there is an overview-view ( =observer ; table from DB) and a detail-view ( =observable ; contains a row from overview-view). 在我的示例中,有一个概述视图( =observer ;来自DB的表)和一个详细视图( =observable ;其中包含来自概述视图的一行)。 Now I want to edit a (in the overview-view) selected row in the detail-view, click save-button, close the detail-view and notify the changes to (all) observers. 现在,我想编辑详细视图中的(在概述视图中)选定的行,单击保存按钮,关闭详细视图并将更改notify给(所有)观察者。

public interface Observer { 
    public void notifyUpdate(Contact contact); 
}

public interface Observable {   
    public void addObserver(Observer observer);
}

public class Detail extends JFrame implements Observable {
    /* multiple observers possible */
    private ArrayList<Observer> observers;
    /* contact will be modified in detail-view and reported to overview-view */
    private Contact contact;
    public Detail() {
        this.observers = new ArrayList<>();
    }
    @Override
    public void addObserver(Observer observer) {
        this.observers.add(observer);
    }
    /* trigger this methode e.g. by save-button, after modifiing contact */ 
    protected void notifyObservers() {
        /* multiple observers possible */
        Iterator<Observer> observerIterator = this.observers.iterator();
        while(observerIterator.hasNext()) {
            /* report observer: constact has modified */
            observerIterator.next().notifyUpdate(contact);
        }       
    }
}

public class Contacts extends JFrame implements Observer {
    private Detail detail;
    public Contacts() {
        detail = new Detail();
        detail.setVisible(true);
        detail.addObserver(this);
    }
    @Override
    public void notifyUpdate(Contact contact) {
        /* notifyUpdate was called from Observable */           
        /* process contact, DB update, update JTable */
    }
}

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

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