如何在不使用CardLayout的情况下在一个框架中从一个面板移动到另一个面板? [英] How to move from one Panel to another in a single frame without using CardLayout?

查看:89
本文介绍了如何在不使用CardLayout的情况下在一个框架中从一个面板移动到另一个面板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序中有三个Panel和一个Frame。我想关闭/隐藏当前面板并显示/激活下一个面板。我正在设计游戏,因此我不想使用 CardLayout

I have three Panels and a single Frame in my program. I want to close/hide the current panel and show/activate the next panel. I am designing a game, hence I do not want to use CardLayout.

private void select() {
    if (currentChoice == 0) {
        f.remove(gpanel1);
        gpanel = new GamePanel();
        f.add(gpanel);
    }
}

这里 f 是框架对象。 gpanel1 gpanel 是初始化的面板对象(已经从框架类调用了构造函数)。

Here f is the frame object. gpanel1 and gpanel are the initialized panel objects (constructors have already been called from the frame class).

如何隐藏旧面板并移至下一个面板?是否有替代CardLayout?

How do I hide the old panel and move to the next panel? Is there an alternative to CardLayout?

推荐答案

要更改JFrame中的面板,请使用以下方法

To change the panel in JFrame use these methods

f.setContentPane(gpanel);
f.pack();

setContentPane()删除当前面板并用提供的面板替换它。

setContentPane() removes current panel and replaces it with the provided panel.

这篇关于如何在不使用CardLayout的情况下在一个框架中从一个面板移动到另一个面板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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