爪哇 - CardLayout秀()抛出:IllegalArgumentException [英] Java - CardLayout show() IllegalArgumentException

查看:380
本文介绍了爪哇 - CardLayout秀()抛出:IllegalArgumentException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与CardLayout show方法问题

I have a problem with the CardLayout show method

所以,我宣布我CardLayout并将其应用到我的JPanel

So I declare my CardLayout and apply it to my JPanel

CardLayout cl = new CardLayout();
panel.setLayout(cl);

然后我可以添加2板到CardLayout

Then I add a 2 panels into the CardLayout

cl.addLayoutComponent(panel, "menuScreen");
cl.addLayoutComponent(panel1, "gameScreen");

然后我有一个JButton可以在点击时,我展示gameScreen

I then have a JButton that when is clicked, I show the gameScreen

public void mouseClicked(MouseEvent e) {
    if(e.getSource() ==  (startGame))

    scenechange.show(panel,"gameScreen");
}

唯一的问题是,它并没有去我gameScreen。这给了我一个llegalArgumentException。
它说:在异常线程AWT-EventQueue的-0java.lang.IllegalArgumentException异常:错误的父级CardLayout。

The only problem is that it doesn't go to my gameScreen. It gives me an llegalArgumentException. It says "Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: wrong parent for CardLayout".

在此先感谢

推荐答案

您需要有三个面板。家长和两张牌。

You need to have three panels. The parent and the two cards.

目前你有板作为家长和童装之一。

Currently you have "panel" as the parent and one of the childen.

CardLayout cl = new CardLayout();
panel.setLayout(cl);

cl.addLayoutComponent(panel, "menuScreen");

考虑从 Java的线索

 ....
 //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);

这篇关于爪哇 - CardLayout秀()抛出:IllegalArgumentException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆