Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am making a game using java. At the beginning of my game there is a panel with a welcome message and then by pressing start you can go to the first level in the game. My question is how to switch panels(to go from the welcome message to level 1). I have found that I can use card layout for that, but I tried with cardlayout, but I have a problem how can I use panels in my cardlayout code from other classes.
Can someone help me with that?

Thank you

What I have tried:

Java
package ColourGame;
 
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
 
public class CardLayOut extends JFrame implements ActionListener{
    CardLayOut card;
    Container c;

    CardLayOut(){
        c=getContentPane();
        c.add(button);    
        c.add(MyPanel);
        c.add(MySecondPanel);
    }
 
    public void actionPerformed(ActionEvent e) {
        card.next(c);
    }

    public static void main(String[] args) {
        CardLayout cl=new CardLayout();  
        cl.setSize(400,400);  
        cl.setVisible(true);  
        cl.setDefaultCloseOperation(EXIT_ON_CLOSE); 
    }
}
Posted
Updated 5-Apr-21 6:41am
v2

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900