Switching between JPanels

后端 未结 3 1211
梦谈多话
梦谈多话 2021-01-21 01:58

I\'m trying to make a game. There\'s several different screens in the game such as a main menu, and the actual game screen. Each of these is a separate jpanel extension. I have

3条回答
  •  北恋
    北恋 (楼主)
    2021-01-21 02:52

    If you want to switch between two panels add those panels into another JPanel and use cardLayout. Then, before adding a JPanel remove the current one. Like this:

    parentPanel.removeAll();
    parentPanel.repaint();
    parentPanel.revalidate();
    
    parentPanel.add(childPanel1);
    parentPanel.repaint();
    parentPanel.revalidate();
    

    Likewise do the same thing for other child panels.

提交回复
热议问题