简体   繁体   中英

CardLayout not working

I'm working on a little game where users login. After you have logged in, you will be able to go to the main menu. I am using a CardLayout which is not working. There are a few final Strings containing the panel names. I have been debugging for a while and I know for sure that this piece of code is reached. Any tips are welcome. Thanks!

/*
 * showPanel()
 * Method to switch to a different panel
 * @author Rick Slinkman
 */
public void showPanel(String newPanel) 
{
    game.setCurrentPanel(newPanel);
    cardLayout.show(mainpanel, newPanel);
    revalidate();
    repaint();
}

The only thing that went wrong was that I did not revalidate my JPanel that holds the cards. This was the solution! Thanks to everyone for taking the effort to help me out.

/*
 * showPanel()
 * Method to switch to a different panel
 * @author Rick Slinkman
 */
public void showPanel(String newPanel) 
{
        game.setCurrentPanel(newPanel);
    this.cardLayout = (CardLayout) cards.getLayout();
    cardLayout.show(cards, "" + newPanel);
    cards.revalidate();
}

如果调用此方法,则代码在任何地方都不需要repaint()。Swing会在看到布局中的更改时重新绘制布局,请删除方法调用repaint。

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