两个不同JFrame之间的通信? [英] Communication between two different JFrames?
问题描述
你好,也许你已经听说过GIMP或类似使用不同帧的东西作为一个完整的gui所以我想知道当两个(可能是多个)帧加载到内存中并且可见时如何进行这种帧通信。
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.
我已经阅读了一些文章,但它们并不那么令人满意,如果有人有一个很好的例子或教程那么请分享。
I have gone through some articles but they were not so satisfactory, if anyone have a good example or tutorial then please share.
问候
Alok sharma
Regards Alok sharma
推荐答案
基本上,这只是在框架中引用框架A的问题B,以及对帧A中帧B的引用:
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 :
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.
这篇关于两个不同JFrame之间的通信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!