I have not been able to find a way to use a CardLayout that works.
I am creating a Pong game. I have a class that extends a JFrame and two classes that extend a JPanel. I am trying to make it so that when the method gets fired in the the first JPanel, it switches from the first JPanel to the second JPanel.
How would I do this, and please provide code.
What you do is use a
CardLayouton the parent component that will hold your twoJPanels. When you add theJPanels to the parent component, you’ll need to provide aStringfor each one, which will be used later to switch between the cards.Then when you want to switch the cards, you need to call a method on the
CardLayoutlayout manager, so you’ll need to get it from the parent component and cast it, or just save a reference to it when you create your parent component. Now to switch the cards: