KeyListeners与Keybindings? [英] KeyListeners vs Keybindings?

查看:101
本文介绍了KeyListeners与Keybindings?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

比较KeyListeners和Key Bindings之间的功能

我一直在尝试在程序中使用KeyListener来获取构造数字的输入。尽管我已经完全实现了它但它无法正常工作但不是问题:P。

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.

无论如何,我按顺序搜索了互联网看看我是否做错了什么,并遇到了Keybindings,它们显然是使用swing组件而不是KeyListeners。从数字键中获取输入会更好吗?

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?

推荐答案

Keylistener是AWT时代的旧界面,它仍然可以使用它与摇摆,但更多的是一般听众。它绑定到所有键。

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有点不同,因为它们专门将特定操作绑定到特定键,其他键不受影响。这样做的结果是,如果你要监听任何密钥,那么KeyListener是合适的,或者你必须为所有密钥实现单独的KeyBindings,这显然是愚蠢的。

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.

为了避免使用具有大switch语句的事件处理程序,KeyBinding存在。

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

这篇关于KeyListeners与Keybindings?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆