I have a JFrame that has two panels, the first panel(panel) has buttons and labels only, where the second panel has the game and keyboard input . The second panel(panelGame) does appear on screen, but with no keyboard response, my space ship cannot move nor I can shoot. Please download my project and help me thank you for your time and consideration.
Asked
Active
Viewed 424 times
-5
-
2Post relevant code here. – Obicere Dec 29 '13 at 06:28
-
You're better off posting your 'panelGame' class code here. How do we know that's not malicious code? Also, have you added a KeyListener to the panel? – blackcompe Dec 29 '13 at 06:30
-
You should consider using key bindings to the frame. – Paul Samsotha Dec 29 '13 at 06:32
-
`test8.java` Line 155 looks like the problem to me. – Elliott Frisch Dec 29 '13 at 06:33
1 Answers
3
From the description of the problem, the likely cause is you are using a KeyListener
of some kind
The problem with this is, a KeyListener
will only be notified of key events when the component it is registered to has key board focus AND is focusable.
A better solution would be to use Key Bindings which allow you to control at what focus level the bindings should be triggered.

trashgod
- 203,806
- 29
- 246
- 1,045

MadProgrammer
- 343,457
- 22
- 230
- 366
-
A related example is seen [here](http://stackoverflow.com/a/14001011/230513). – trashgod Dec 29 '13 at 10:31