比较 KeyListener 和 Key Bindings 之间的功能

发布于 2024-12-23 04:40:24 字数 438 浏览 2 评论 0原文

当一位匿名用户否决了我涉及 KeyListeners 的答案并建议改用 Key Bindings 时,就出现了这个问题。这位匿名用户告诉我,KeyListener 接口是一个旧的 AWT 解决方案,不应使用。

然而,我不知道我是否应该完全相信这些信息。我在包括 Oracle 在内的各种网站上进行了研究,但没有发现任何有关 KeyListeners 或 Key Bindings 功能的信息。我知道两者执行相似的任务,但可以这么说,我不确定“幕后”到底发生了什么。

我倾向于在未来的项目中使用键绑定,只是因为我获得的研究表明 KeyListener 接口要求相关组件具有焦点,而键绑定则不需要。但是,我很困惑。为什么会这样呢?键绑定的触发方式与 KeyListener 的触发方式有何不同?

PS 我很确定这种情况很少见,但是在某些情况下使用 KeyListener 更合适吗?

This question arose when an anonymous user downvoted an answer of mine involving KeyListeners and suggested the use of Key Bindings instead. This anonymous user informed me that the KeyListener interface was an old AWT solution and should not be used.

However, I don't know if I should trust that information completely. I have researched both on various websites, oracle included, and found nothing regarding the functionality of KeyListeners or Key Bindings. I am aware of the fact that the two perform similar tasks, but am unsure of exactly what goes on "behind the scenes", so to speak.

I'm kind of leaning towards using Key Bindings in future projects, simply because I acquired research suggesting that the KeyListener interface required that the component in question have focus while Key Bindings did not. But, I am confused. Why is this so? How are Key Bindings triggered differently than KeyListeners?

P.S. I'm pretty sure this is a rarity, but are there some circumstances where using KeyListeners is more appropriate?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

顾忌 2024-12-30 04:40:24

如何使用键绑定解释了基础知识。

绑定用于将特定操作映射到特定击键。

当所有其他抽象都不起作用时,KeyListener 是最后的手段。当您监听任何 KeyStroke 时,也许您会使用 KeyListener。因此,为字母表中的所有字母创建 26 个绑定是不切实际的。

但在很多情况下,还是有更好的 API 可供使用。例如,您可以使用 DocumentListener 来侦听文档的更改,而不是侦听文本字段上的 KeyEvent。

How to Use Key Bindings explains the basics.

Bindings are used to map specific Actions to a specific KeyStroke.

A KeyListener is the last resort when all other abstractions won't work. Maybe you would use a KeyListener when you are listening for ANY KeyStroke. So it wouldn't be practical to create 26 bindings for all letters of the alphabet.

But again in many cases there are better API's to use. For example, instead of listening for KeyEvents on a text field you would use a DocumentListener to listen for changes to the Document.

不弃不离 2024-12-30 04:40:24

我建议您使用 KeyBindings 的原因有很多:

  • 它实际上比 KeyListener 工作得更快。
  • KeyBinding 更加发达,并且可以提供更多的可能性。
  • 虽然您需要使用 Swing 计时器,但它更适合游戏。
  • 是可设置和可共享的。

除了 KeyListener 之外使用它可能很困难,但 KeyListener 将无法处理游戏中的多个按键。

I would suggest you to use KeyBindings for many reasons:

  • It actually works faster than a KeyListener.
  • KeyBinding is more developed and has much more to offer as possibilities.
  • Although you would need to use a Swing timer, it is better for games.
  • Is is settable and shareable.

It can be difficult to use it aside to a KeyListener but a KeyListener will not be able to process multiple key presses in a game.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文