简体   繁体   中英

java wrong parent for CardLayout

This code has f1(frame), p1(panel){has button b1,b111}, p2(panel){has button b2}, p3(panel){has button b3}

f1 has p1,p2,p3

I want to change panel using to CardLayout If button clicked , panel will change.

but When I execute it the following exception is thrown

Wrong parent for CardLayout .

please somebody help me

   import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    class MyActionListener extends event implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            executive ex = new executive();
            JButton b = (JButton) e.getSource();
            f1.getContentPane().removeAll();
            if (b.getText().equals("초시계")){
                card.show(f1,"p2");
            }
            else if(b.getText().equals("다음화면")){
                card.show(f1,"p3");
            }
            else{
                card.show(f1,"p1");
            }
            revalidate();
            repaint();
            f1.pack();
         }
    }
    class event extends JFrame{
        static JFrame f1 = new JFrame("event");
        static JButton b1 = new JButton("초시계");
        static JButton b111 = new JButton("다음화면");
        static JButton b2 = new JButton("처음화면");
        static JButton b3 = new JButton("처음화면");
        static JLabel l1 = new JLabel("success");
        static JPanel p1 = new JPanel();
        static JPanel p2 = new JPanel();
        static JPanel p3 = new JPanel();
        static CardLayout card = new CardLayout(0, 0);
        public static void main(String [] args){
            f1.setLocation(200,400);
            f1.setPreferredSize(new Dimension(600,400));
            f1.setLayout(card);
            p1.add(b1,BorderLayout.SOUTH);
            p1.add(b111,BorderLayout.SOUTH);
            p2.add(b2,BorderLayout.SOUTH); 
            p3.add(b3,BorderLayout.SOUTH);
            f1.add(p1,"p1");
            f1.add(p2,"p2");
            f1.add(p3,"p3");
            f1.pack(); 
            f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
            f1.setVisible(true);
            MyActionListener my = new MyActionListener();
            executive ex = new executive();
            b1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    my.actionPerformed(e);
                }
            });
            b111.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    my.actionPerformed(e);
                }
            });
            b2.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    my.actionPerformed(e);
                }
            });
            b3.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    my.actionPerformed(e);
                }
            });
        }
    }

First: with

f1.getContentPane().removeAll();

you remove all panels from the content pane. Don't do this - you use the CardLayout to manage the contents!

Second: the CardLayout only manages the content pane of the frame, not the whole frame. To switch between the panes, you have to use:

        if (b.getText().equals("초시계")){
            card.show(f1.getContentPane(),"p2");
        }
        else if(b.getText().equals("다음화면")){
            card.show(f1.getContentPane(),"p3");
        }
        else{
            card.show(f1.getContentPane(),"p1");
        }

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