Java animation, with key listener

2019-08-31 05:13发布

问题:

I tried to make it so that,if I pressed the right/left key, the sprite/Mario would face right/left. If I pressed the right key, he would face right. But for some reason, When I pressed the left key, he won't face left.

Source Code: First ,Second

Images

回答1:

"To fire keyboard events, a component must have the keyboard focus."—How to Write a Key Listener



回答2:

Don't use a KeyListener. Instead use Key Bindings which are more flexible and are used by all Swing components.