问题
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