简体   繁体   中英

Change JPanel at runtime with a button

Hello I have a main layout made in this way:


            |         |
Main pane   |Menu pane|
            |         |

Now, menu pane is made just of button: clicking on a button switches main panel with another panel. On click event is made this way:

public void actionPerformed(ActionEvent evt){
     mainPanel = new MyNewPanel();
     this.revalidate();
}

But, for some reason, main panel does not change!

You are not setting the main component of your container. You need to add your new panel and call validate() on that container.

Note, depending on your the layout of your container you may need to remove the currently visible component first.

CardLayout could manage all this for you.

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