Cg2916
Cg2916

Reputation: 1117

How To Start A JPanel After Switching

I am creating a Pong clone with a menu and the actual game itself. I am using a CardLayout to switch between the two JPanels.

However, when I switch from the menu to the board (or playing area), the board just sits there and won't accept my keyboard inputs. How do I start it or start the key listening?

Source code: https://[email protected]/cg2916/Pong.git

Upvotes: 0

Views: 238

Answers (3)

Mosha
Mosha

Reputation: 1

for CardLayout the listener should have these codes

 CardLayout card=(CardLayout) JPanel_with_cardLayout.getLayout();
 card.show(JPanel_with_cardLayout,"cardname of card you want to switch");

example:

CardLayout card=(CardLayout) JPanel2.getLayout();
card.show(JPanel2,"card4");

Upvotes: 0

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

Answer (as recommended to you previously): Don't use a KeyListener. use Key Bindings.

Upvotes: 2

rtheunissen
rtheunissen

Reputation: 7435

requestFocusInWindow() should do the trick.

Upvotes: 1

Related Questions