我正在开发一个用户登录的小游戏。登录后,您将能够进入主菜单。我使用的CardLayout不起作用。有一些包含面板名称的最后一个字符串。我已经调试了一段时间,我确信这段代码是可以实现的。欢迎任何建议。谢谢!
/*
* 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();
}发布于 2012-08-11 04:29:55
唯一出错的是我没有重新验证我的JPanel。这就是解决方案!感谢大家的努力,帮助我走出困境。
/*
* 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();
}发布于 2012-08-11 04:12:50
您的代码在任何地方都不需要repaint()如果您调用此方法Swing将在看到布局中的更改时重新绘制布局,请删除调用repaint的方法。
https://stackoverflow.com/questions/11908194
复制相似问题