打开两个面板 [英] Switch two panels in on

查看:128
本文介绍了打开两个面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含2个面板的J框架,我可以使用Visible在它们之间切换,但我希望它们出现在相同的位置和相同的尺寸。

I have got one J Frame that includes 2 panels , I was able to switch between them using Visible but I want them to appear in the same position and the same size the other was in.

推荐答案

使用CardLayout交换JPanel。可以在此处找到该教程: CardLayout教程

Use a CardLayout to swap JPanels. The tutorial can be found here: CardLayout tutorial.

当你这样做时,你需要设置一个JPanel来使用CardLayout,它将保存你的另外两个JPanel。您需要使用带有String常量的JPanel将这些JPanel添加到CardLayout,以便CardLayout能够使用String标识视图。例如:

When you do this, you will need a JPanel to be set to use the CardLayout and which will hold your other two JPanels. You will need to add these JPanels to the CardLayout using JPanel with String constants, so that the CardLayout will be able to identify the views with a String. For instance:

CardLayout cardLayout = new CardLayout();
JPanel cardHoldingPanel = new JPanel(cardLayout);

// .....

然后添加两个视图到上面:

then when adding your two views to the above:

cardHoldingPanel.add(viewPanelOne, "one");
cardHoldingPanel.add(viewPanelTwo, "two");

然后交换视图,如果你只有两个JPanel,你可以简单地调用<$ c CardLayout对象上的$ c> next(...)

Then to swap views, if all you have are the two JPanels, you could simply call next(...) on the CardLayout object

cardLayout.next(cardHoldingPanel);

要显示特定视图,您需要使用String常量来添加视图并调用<$在CardLayout对象上c $ c> show(...):

To show a specific view, you would need the String constant used to add the view and call show(...) on the CardLayout object:

cardLayout.show(cardHoldingPanel, "one");

这篇关于打开两个面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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