KeyListeners vs Keybindings? [duplicate]

眉间皱痕 提交于 2020-01-11 11:22:08

问题


Possible Duplicate:
Comparing functionality between KeyListeners and Key Bindings

I've been trying to use the KeyListener in my program in order to get the input for constructing a number. It doesn't work as far as I can tell even though i've fully implemented it but thats not the question :P.

Anyway, I searched the internet in order to see if I was doing something wrong and came across Keybindings which are apparently made to work with swing components as opposed to KeyListeners. Which would be better for just getting the input from the number keys?


回答1:


The Keylistener is an older interface from the AWT days, its still Ok to use it with swing but is more of a general listener. It binds to all keys.

KeyBindings are a bit different in that they specifically bind a specific action to a specific key and other keys remain unaffected. The upshot of this is if you are going to listen for any key then a KeyListener is appropriate or you will have to implement seperate KeyBindings all for all keys which is patently silly.

To avoid event handlers with big switch statements the KeyBinding exists.



来源:https://stackoverflow.com/questions/10035980/keylisteners-vs-keybindings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!