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

查看:342
本文介绍了如何动态更改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?

推荐答案

你的用例,对于 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天全站免登陆