简体   繁体   English

处置当前jframe时如何设置上一个jframe可见

[英]How to set the previous jframe visible when current jframe is disposed

I am making a Java gui project and it consists of two frames. 我正在制作一个Java gui项目,它由两个框架组成。

The problem is that when I call the secondframe from the firstframe, I have set it such that the firstframe visibility is set to false. 问题是,当我从第一帧调用第二帧时,已将其设置为使第一帧可见性设置为false。 The problem is how do I make the firstframe visible again by using a button from the second frame. 问题是如何使用第二帧中的按钮使第一帧再次可见。

should i ditch this method and create a new jpanel instead??? 我应该放弃这种方法,而是创建一个新的jpanel吗??? Does jpanel have similar capabilities as jframe? jpanel是否具有与jframe类似的功能?

Consider using CardLayout . 考虑使用CardLayout This way you can switch via multiple UIs without needing another frame. 这样,您可以通过多个UI进行切换,而无需其他框架。 Here's how to use it. 这是使用方法。

Edit: As Guillaume posted in his comment, this answer from Andrew also covers how to use the layout. 编辑:作为纪尧姆张贴在他的评论中, 从安德鲁的回答也介绍了如何使用的布局。

Edit2: EDIT2:
As you requested a little more information about my latest post, here's how such a class may look like: 当您请求有关我的最新帖子的更多信息时,此类课程的外观如下:

import javax.swing.JFrame;


public abstract class MyFrameManager {
    static private JFrame   startFrame,
                        anotherFrame,
                        justAnotherFrame;

static public synchronized JFrame getStartFrame()
{
    if(startFrame == null)
    {
        //frame isnt initialized, lets do it
        startFrame = new JFrame();
        startFrame.setSize(42, 42);
        //...
    }

    return startFrame;
}

static public synchronized JFrame getAnotherFrame()
{
    if(anotherFrame == null)
    {
        //same as above, init it
    }

    return anotherFrame;
}

static public synchronized JFrame getJustAnotherFrame()
{
    //same again
    return justAnotherFrame;
}

public static void main(String[] args) {
    //let's test!

    JFrame start = MyFrameManager.getStartFrame();
        start.setVisible(true);

    //want another window
    JFrame another = MyFrameManager.getAnotherFrame();
        another.setVisible(true);

    //oh, doenst want start anymore
    start.setVisible(false);
}
}

This way you would only instantiate every JFrame once, but you could always access them via your manager class. 这样,您只需实例化每个JFrame一次,但始终可以通过管理器类访问它们。 What you do with them after that is your decision. 之后,您对他们的处理是您的决定。
I also just made it thread-safe, which is crucial for singletons. 我还使它成为线程安全的,这对于单例来说至关重要。

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

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