Changing Panels using the Card layout [closed]
Asked Answered
P

1

19

Hi Im sorry if this has already been posted but I looked hard and found other codes that I could not understand very well. I am new to java programming and would like someone to point me in the right direction on how I could change to desired panels by pressing a button. any help would be highly appreciated. I am just trying to fully understand how this concept works.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;  // Packages used in this program imported    

public class onlineGame extends JFrame implements ActionListener
{

    JPanel cards;
    JButton button1, button2, button3;

    public onlineGame() //This is the CONSTRUCTOR method
    {
        //The entry point into your program
        setLayout(new FlowLayout()); //Use this for now.
        setSize(810, 510); //Set the size of the JFrame
        setTitle("Generic Card Game"); //Put Title on top of JFrame
        setBackground(Color.yellow);
        setResizable(false);

        button1 = new JButton("THIS IS BUTTON 1");
        button2 = new JButton("THIS IS BUTTON 2");
        button3 = new JButton("THIS IS BUTTON 3");

        button1.addActionListener(this);
        button2.addActionListener(this);
        button3.addActionListener(this);    

        //Create the cards

        JPanel card1 = new JPanel();
        card1.add(button1);

        JPanel card2 = new JPanel();
        card2.add(button2);

        JPanel card3 = new JPanel();
        card3.add(button3);

        //Create the panel that contains the "cards".

        cards = new JPanel(new CardLayout());

        cards.add(card1);
        cards.add(card2);
        cards.add(card3);

        getContentPane().add(cards); 
        setVisible(true); //Make JFrame visible
    }

    public void actionPerformed(ActionEvent e)
    {    
        if (e.getSource() == button1){    
            //What do i put here to change to Panel card2 or card3 and so on.
        }
    }
    public static void main(String args[])
    {    
        new onlineGame(); // This calls the constructor and runs it    
    }
}
Psychodynamics answered 11/2, 2013 at 22:16 Comment(2)
(For future visitors) The Oracle documentation for CardLayout is pretty easy to understand.Petey
Absolutely no reason to close this. In no way is this a narrow question. It's as broad as questions come.Hereon
D
37

To switch to an arbitrary panel make sure to specify the constraints when adding the card components:

cards.add(card1, "Card 1");
cards.add(card2, "Card 2");
cards.add(card3, "Card 3");

then to flip to a different component:

CardLayout cardLayout = (CardLayout) cards.getLayout();
cardLayout.show(cards, "Card 2");

To navigate to the next component, you can use:

cardLayout.next(cards);

Read: CardLayout

Deliciadelicious answered 11/2, 2013 at 22:23 Comment(4)
Thank you. Can I specify which panel I would like to change it to rather than it changing to the next one down.Psychodynamics
@aboadam Yes. Reimeus has shown you exactly how to achieve that. Instead of using next, you would us CardLayout#show(Container, String)Ious
Does the object of card1 "know" about cards object?Ivar
if by "know" you mean is there a way to get the parent container reference, then that is found using card1.getParent()...Deliciadelicious

© 2022 - 2024 — McMap. All rights reserved.