简体   繁体   中英

Java cardLayout wrong parent

I know this question has been asked before but i cant seem to make the answers work, nor understand them.

What i am trying to do is to swap panels which button click. This is the my main function:

public class CreateWindow extends JFrame{

    public CreateWindow() {
        this.setTitle("Test GUI");

        this.putIcon();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(512, 512);

        this.add(new CreatePanel());

        this.setVisible(true);
    }

and this is where i try to create the panels:

public class CreatePanel extends JPanel implements ActionListener {

    JTextArea directoryText;
    JFileChooser chooser;
    String directory;
    ArrayList<File> files;
    CardLayout cards;
    JPanel panel1, panel2;

    public CreatePanel() {
        this.setSize(256, 256);
        cards = new CardLayout();
        this.setLayout(cards);

        files = new ArrayList<>();
        panel1 = new JPanel(null);
        panel2 = new JPanel(null);

        this.add(panel1, "panel1");
        this.add(panel2, "panel2");
        panel1.setBackground(Color.red);
    panel2.setBackground(Color.blue);

        cards.show(panel1, "panel1");

    }
}

And i keep on getting the following error:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: wrong parent for CardLayout
    at java.awt.CardLayout.checkLayout(CardLayout.java:404)
    at java.awt.CardLayout.show(CardLayout.java:526)
    at jeasussaves.CreatePanel.<init>(CreatePanel.java:54)
    at jeasussaves.CreateWindow.<init>(CreateWindow.java:29)
    at jeasussaves.JeasusSaves$1.run(JeasusSaves.java:22)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:749)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:702)
    at java.awt.EventQueue$3.run(EventQueue.java:696)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:719)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

CardLayout#show(Container, String) requires you to specify the "deck" you want to use as the first parameter, then a "card" from that deck as the second parameter.

In this case, your JPanel (the one being extended) is the deck (since it's the one using CardLayout ) and panel1 is a card, since you added it to the deck. Change your cards.show(panel1, "panel1") to cards.show(this, "panel1") .

There's no need to display your first card after adding all the cards. The first card you add is the first card to be displayed

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