我的 CardLayout 显示方法有问题
所以我声明我的 CardLayout 并将其应用到我的 JPanel
CardLayout cl = new CardLayout();
panel.setLayout(cl);
然后我在 CardLayout 中添加 2 个面板
cl.addLayoutComponent(panel, "menuScreen");
cl.addLayoutComponent(panel1, "gameScreen");
然后我有一个 JButton,单击它时,我会显示游戏屏幕
public void mouseClicked(MouseEvent e) {
if(e.getSource() == (startGame))
scenechange.show(panel,"gameScreen");
}
唯一的问题是它没有进入我的游戏屏幕。它给了我一个 llegalArgumentException。 它显示“线程“AWT-EventQueue-0”java.lang.IllegalArgumentException 中出现异常:CardLayout 的父级错误”。
提前致谢
最佳答案
您需要三个面板。家长和两张卡。
目前,您有“panel”作为父级和子级之一。
CardLayout cl = new CardLayout();
panel.setLayout(cl);
和
cl.addLayoutComponent(panel, "menuScreen");
考虑 Java trail 中的代码示例
....
//Create the "cards".
JPanel card1 = new JPanel();
card1.add(new JButton("Button 1"));
JPanel card2 = new JPanel();
card2.add(new JTextField("TextField", 20));
//Create the panel that contains the "cards".
cards = new JPanel(new CardLayout());
cards.add(card1, BUTTONPANEL);
cards.add(card2, TEXTPANEL);
关于Java - CardLayout show() IllegalArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12290609/