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.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Use a CardLayout to swap JPanels. The tutorial can be found here: CardLayout tutorial.
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");
Then to swap views, if all you have are the two JPanels, you could simply call next(...)
on the CardLayout object
cardLayout.next(cardHoldingPanel);
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");