Switch two panels in on

筅森魡賤 提交于 2019-12-02 13:19:32

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");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!