Trick for java applets

I have created a java game based on JPanel. When I add it to the JFrame it works great and when I add it to the JApplet and test it with the built in eclipse tester it works great. However, when I try to run the applet through the html site, it doesn't work. It loads but doesn't require keyboard input. I have set where "pressing s" starts the game, but even when I click on the game and press s nothing happens.

Do I need to set the keyboard focus because I thought it was done automatically.

+2


source to share


1 answer


but it doesn't accept keyboard input.

KeyEvents are only passed to the focused component. I would assume that your panel has no focus, so make sure the panel is focused and then use the requestFocusInWindow () method when the GUI is visible to make sure the panel has focus.



However, the best solution to the problem is not to rely on the KeyListener, but to use key bindings. Swing was designed to use key bindings.

Learn more about key bindings .

+1


source







All Articles