JPanel not responding to keylistener

后端 未结 2 1133
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-24 11:57

I tried to make a JPanel that would display how many times you press each navigation key, but it is only displaying 4 zeros.

//In the keyPressed() method, I put the foll

2条回答
  •  南方客
    南方客 (楼主)
    2021-01-24 12:28

    Welcome to the wonderful world of KeyListeners.

    KeyListeners by design, can only respond to key events when the component they are registered to is focusable AND has focus.

    This generally makes them a poor choice in a gaming environment, where you might have a number of components that can steal focus from you main game component.

    Instead, you should be using Key bindings

提交回复
热议问题