如何动态更改 JFrame 内的 JPanel? [英] How do I change JPanel inside a JFrame on the fly?

查看:44
本文介绍了如何动态更改 JFrame 内的 JPanel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单来说,有一个简单的 java swing 应用程序,它由 JFrame 和一些组件组成.其中一个组件是 JPanel,旨在根据用户操作由另一个 JPanel 替换.

To put it simple, there's a simple java swing app that consists of JFrame with some components in it. One of the components is a JPanel that is meant to be replaced by another JPanel on user action.

那么,做这种事情的正确方法是什么?我试过了

So, what's the correct way of doing such a thing? I've tried

panel = new CustomJPanelWithComponentsOnIt();
parentFrameJPanelBelongsTo.pack();

但这行不通.你有什么建议?

but this won't work. What would you suggest?

推荐答案

你的用例,看起来很适合 卡片布局.

Your use case, seems perfect for CardLayout.

在卡片布局中,您可以在同一位置添加多个面板,然后一次显示或隐藏一个面板.

In card layout you can add multiple panels in the same place, but then show or hide, one panel at a time.

这篇关于如何动态更改 JFrame 内的 JPanel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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