当设置当前jframe时如何设置上一个jframe可见 [英] How to set the previous jframe visible when current jframe is disposed

查看:97
本文介绍了当设置当前jframe时如何设置上一个jframe可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个Java gui项目,它由两个框架组成.

I am making a Java gui project and it consists of two frames.

问题是,当我从第一帧调用第二帧时,我已对其进行了设置,以使第一帧可见性设置为false.问题是如何使用第二帧中的按钮使第一帧再次可见.

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. The problem is how do I make the firstframe visible again by using a button from the second frame.

我应该放弃这种方法,而是创建一个新的jpanel吗??? jpanel是否具有与jframe类似的功能?

should i ditch this method and create a new jpanel instead??? Does jpanel have similar capabilities as jframe?

推荐答案

考虑使用CardLayout.这样,您可以通过多个UI进行切换,而无需其他框架. 这里是使用方法.

Consider using CardLayout. This way you can switch via multiple UIs without needing another frame. Here's how to use it.

正如纪尧姆(Guillaume)在他的评论中所说,

As Guillaume posted in his comment, this answer from Andrew also covers how to use the layout.


当您要求提供有关我的最新帖子的更多信息时,此类课程的外观如下:


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);
}
}

这样,您只需实例化每个JFrame一次,但是您始终可以通过您的经理类访问它们.之后,您对他们的处理就是您的决定.
我还使它成为线程安全的,这对于单身人士至关重要.

This way you would only instantiate every JFrame once, but you could always access them via your manager class. What you do with them after that is your decision.
I also just made it thread-safe, which is crucial for singletons.

这篇关于当设置当前jframe时如何设置上一个jframe可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆