简体   繁体   中英

java - how to replace a jPanel based on user clicks

i have a jPanel containing 3 menus--lets call it MenuPanel i place it on the top of my jFrame (border_layout top). i have also 3 jPanels (PanelOne, PanelTwo, PanelThree) and i want it to be showed on the bottom of the jFrame (border_layout bottom) based on the menu that the user choose

Simple solution: use a CardLayout to help you swap JPanels.

  • Place a JPanel that uses CardLayout in the BorderLayout.SOUTH position, say called cardHolderPanel.
  • Add the three "card" JPanels into the cardHolderPanel using unique String constants.
  • Swap the cards by calling show(cardHolderPanel, APPROPRIATE_STRING_CONSTANT) on your CardLayout.

Tutorial: http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM