ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java中的键绑定与键侦听器

2019-09-15 17:00:15  阅读:269  来源: 互联网

标签:keylistener key-events java swing key-bindings


我注意到在Java / Swing中,似乎至少有两种不同的方法来处理关键事件:

> Key Bindings
> Key Listeners

每个人的优点/缺点是什么,何时你更喜欢一个而不是另一个?

解决方法:

when should you prefer one rather than the other?

因为它们被引入所以更喜欢Key Bindings. KeyListener是与事件的较低级别连接.

key bindings的那个页面涵盖了我倾向于使用它们而不是KeyListener的很多原因.它列出了许多对KeyListener来说简单“不可用”的东西.例如.选择:

> WHEN_FOCUSED
> WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
> WHEN_IN_FOCUSED_WINDOW

我阅读链接文档越多,我就越不能理解提出问题的必要性.例如.:

An alternative to key bindings is using key listeners. Key listeners have their place as a low-level interface to keyboard input, but for responding to individual keys key bindings are more appropriate and tend to result in more easily maintained code. Key listeners are also difficult if the key binding is to be active when the component doesn’t have focus. Some of the advantages of key bindings are they’re somewhat self documenting, take the containment hierarchy into account, encourage reusable chunks of code (Action objects), and allow actions to be easily removed, customized, or shared. Also, they make it easy to change the key to which an action is bound. Another advantage of Actions is that they have an enabled state which provides an easy way to disable the action without having to track which component it is attached to.

文字组件

如@Robin所述,文本组件也有DocumentListener& DocumentFilter可以添加更适合文本文档的功能.有关文件listeners&amp ;;的更多信息,请参见Text Component Features. filters.

标签:keylistener,key-events,java,swing,key-bindings
来源: https://codeday.me/bug/20190915/1805513.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有