How to Use A CardLayout in My Situation

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.

Answers


What you do is use a CardLayout on the parent component that will hold your two JPanels. When you add the JPanels to the parent component, you'll need to provide a String for each one, which will be used later to switch between the cards.

CardLayout cardLayout = new CardLayout();
JPanel parentComponent = new JPanel(cardLayout);
parentComponent.add( jPanel1, "Panel 1" );
parentComponent.add( jPanel2, "Panel 2" );

Then when you want to switch the cards, you need to call a method on the CardLayout layout 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:

cardLayout.show( parentComponent, "Panel 1" ); // Shows panel 1
cardLayout.show( parentComponent, "Panel 2" ); // Shows panel 2

Need Your Help

Rotate an image around center

android

How can I rotate an image around it's center point? This rotates it but also moves it:

Is controller scaffolding missing in MVC 6?

c# asp.net-mvc-6 asp.net-mvc-scaffolding vs-2015-preview

When creating controller in MVC 6 I don't see the scaffolding for creating controller methods? Will they be missing or in the production release?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.