如何动态地将按键分配给按钮?
我的 GUI 中有一个部分是根据对象列表动态生成的。 因此,对于该列表中的每个对象,我想创建一个 JButton 并关联一个键盘快捷键。
例如:
for (String tag : testTags) {
new JButton(tag).setMnemonic(KeyEvent.VK_F1);
}
如何以优雅的方式使代码“setMnemonic(KeyEvent.VK_F1)”动态化?有没有某种方法可以自动获取一系列键,然后在本次迭代中使用它?
谢谢!
I have a section in my GUI that is generated dynamically according to a list of objects.
So, for each object in that list I want to create a JButton and associate a keyboard shortcut.
For example:
for (String tag : testTags) {
new JButton(tag).setMnemonic(KeyEvent.VK_F1);
}
How do I make the code "setMnemonic(KeyEvent.VK_F1)" dynamic in an elegant way? Is there some way to get a range of keys automatically and then use it in this iteration?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
操作
非常适合于此。请参阅如何使用操作更多的。An
Action
is well suited for this. See How to Use Actions for more.只需迭代接受的整数范围即可。
Just iterate through the range of accepted ints.
创建一个包含您的键的数组
,或者迭代 F1-F12 键的范围 (112 - 123),
但您必须检查 键 是否仍在范围内(123 是 F12)。
Either create an array containing your Keys with
or iterate over the range of the F1-F12 keys (112 - 123)
You do have to check if key is still in range (123 being F12), though.