How to Change java Cardlayout from another separate class

后端 未结 1 1344
礼貌的吻别
礼貌的吻别 2020-12-04 02:52

Please I have been trying to switch CardLayout from another class (JPanel) which is one of the card on the CardLayout, I have search and made research about this for a very

相关标签:
1条回答
  • 2020-12-04 03:45

    In your provided code, you never added FirstCard and SecondCard, to the JPanel having layout set to CardLayout. Since what you writing is this :

    jPanel1.add(jPanel2, "card2");
    

    here jPanel2 is an instance of JPanel, as you have initialized this in your TestmyClass Class, as :

    jPanel2 = new javax.swing.JPanel();
    

    instead I guess what you should be writing is :

    jPanel2 = new SecondCard(passPanelWithCardLayoutAsArgument); // So that you can manoeuvre around b/w other JPanels
    

    Here is a small working example for your help :

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class CardLayoutExample
    {
        private JPanel contentPane;
        private MyPanel panel1;
        private MyPanel panel2;
        private MyPanel panel3;
    
        private void displayGUI()
        {
            JFrame frame = new JFrame("Card Layout Example");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JPanel contentPane = new JPanel();
            contentPane.setBorder(
                BorderFactory.createEmptyBorder(5, 5, 5, 5));
            contentPane.setLayout(new CardLayout());
    
            panel1 = new MyPanel(contentPane
                    , Color.RED.darker().darker());
            panel2 = new MyPanel(contentPane
                    , Color.GREEN.darker().darker());
            panel3 = new MyPanel(contentPane
                    , Color.DARK_GRAY); 
    
            contentPane.add(panel1, "Panel 1"); 
            contentPane.add(panel2, "Panel 2");
            contentPane.add(panel3, "Panel 3");         
    
            frame.setContentPane(contentPane);      
            frame.pack();   
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        public static void main(String... args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new CardLayoutExample().displayGUI();
                }
            });
        }
    }
    
    class MyPanel extends JPanel 
    {
    
        private JButton jcomp1;
        private JPanel contentPane;
        private Color backgroundColour;
    
        public MyPanel(JPanel panel, Color c) 
        {   
            contentPane = panel;
            backgroundColour = c;
    
            setOpaque(true);
            setBackground(backgroundColour);
    
            //construct components
            jcomp1 = new JButton ("Show New Panel");
            jcomp1.addActionListener( new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    CardLayout cardLayout = (CardLayout) contentPane.getLayout();
                    cardLayout.next(contentPane);
                }
            });
    
            add(jcomp1);
        }
    
        @Override
        public Dimension getPreferredSize()
        {
            return (new Dimension(500, 500));
        }
    }
    

    LATEST EDIT : *Using your components and trying to put that into CardLayout, with this code : *

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class CardLayoutExample
    {
        private JPanel contentPane;
        private FirstCard panel1;
        private SecondCard panel2;
    
        private void displayGUI()
        {
            JFrame frame = new JFrame("Card Layout Example");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JPanel contentPane = new JPanel();
            contentPane.setBorder(
                BorderFactory.createEmptyBorder(5, 5, 5, 5));
            contentPane.setLayout(new CardLayout());
    
            panel1 = new FirstCard(contentPane);
            panel2 = new SecondCard(contentPane);
    
            contentPane.add(panel1, "Panel 1"); 
            contentPane.add(panel2, "Panel 2");     
    
            frame.setContentPane(contentPane);      
            frame.pack();   
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        public static void main(String... args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new CardLayoutExample().displayGUI();
                }
            });
        }
    }
    
    class FirstCard extends javax.swing.JPanel 
    {
        private javax.swing.JTextField addField;
        private javax.swing.JTextField nameField;
        private javax.swing.JTextField occField;
        private javax.swing.JTextField phoneField;
        private javax.swing.JLabel nameLabel;
        private javax.swing.JLabel addLabel;
        private javax.swing.JLabel occLabel;
        private javax.swing.JLabel phoneLabel;
        private JPanel centerPanel;
        private JPanel contentPane;
        private JButton nextButton;
    
        public FirstCard(JPanel cp) 
        {
            this.contentPane = cp;
            initComponents();
        }
    
        private void initComponents() 
        {
            setOpaque(true);
            setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
            setBackground(Color.RED);
            setLayout(new BorderLayout(5, 5));
    
            nameLabel = new javax.swing.JLabel("Guarantee Name : ");
            nameField = new javax.swing.JTextField();   
            addLabel = new javax.swing.JLabel("Address : ");
            addField = new javax.swing.JTextField();        
            occLabel = new javax.swing.JLabel("Occupation : ");
            occField = new javax.swing.JTextField();
            phoneLabel = new javax.swing.JLabel("Phone : ");
            phoneField = new javax.swing.JTextField();
            centerPanel = new JPanel();
            nextButton = new JButton("Next");
            nextButton.addActionListener(new ActionListener()
            {
                @Override
                public void actionPerformed(ActionEvent ae)
                {
                    nextButtonAction(ae);
                }
            });
    
            centerPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
            centerPanel.setOpaque(true);
            centerPanel.setBackground(Color.WHITE);
            centerPanel.setLayout(new GridLayout(0, 2, 5, 5));
            centerPanel.add(nameLabel);
            centerPanel.add(nameField);
            centerPanel.add(addLabel);
            centerPanel.add(addField);
            centerPanel.add(occLabel);
            centerPanel.add(occField);
            centerPanel.add(phoneLabel);
            centerPanel.add(phoneField);
    
            add(centerPanel, BorderLayout.CENTER);
            add(nextButton, BorderLayout.PAGE_END);
        }                   
    
        private void nextButtonAction(ActionEvent ae)
        {
            CardLayout layout = (CardLayout)contentPane.getLayout();
            layout.next(contentPane);
        }
    }
    
    class SecondCard extends javax.swing.JPanel 
    {
    
        private javax.swing.JButton nextButton;
        private javax.swing.JLabel textLabel;
        private JPanel contentPane;
    
        public SecondCard(JPanel cp) 
        {
            contentPane = cp;
            initComponents();
        }
    
        private void initComponents() 
        {
    
            setOpaque(true);
            setBackground(Color.GREEN.darker().darker());
            setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
            textLabel = new JLabel("this is the second card", JLabel.CENTER);
            textLabel.setForeground(Color.WHITE);
            nextButton = new javax.swing.JButton();
    
            nextButton.setText("SwitchCard");
            nextButton.addActionListener(new ActionListener() 
            {
                public void actionPerformed(ActionEvent evt) 
                {
                    jButton1ActionPerformed(evt);
                }
            });
    
            add(textLabel);
            add(nextButton);
        }
    
        private void jButton1ActionPerformed(ActionEvent evt) 
        {
            CardLayout layout = (CardLayout) contentPane.getLayout();
            layout.show(contentPane, "Panel 1");
        }
    }
    

    Method to clear fields

    private void clearFields()
    {
        Component components[] = centerPanel.getComponents();
        for (Component comp : components)
        {
            if (comp instanceof JTextField)
            {
                JTextField tfield = (JTextField) comp;
                tfield.setText("");
            }
            else if (comp instanceof JComboBox)
            {
                JComboBox cbox = (JComboBox) comp;
                cbox.setSelectedIndex(0);
            }
            else if (comp instanceof JRadioButton)
            {
                JRadioButton rbut = (JRadioButton) comp;
                rbut.setSelected(false);
            }
        }
    }
    

    And you will call this inside the actionPerformed() method of the Button, which will take you to the next Card.

    0 讨论(0)
提交回复
热议问题