无论JComponent是什么焦点,你如何为JFrame进行键绑定? [英] How do you make key binding for a JFrame no matter what JComponent is in focus?

查看:191
本文介绍了无论JComponent是什么焦点,你如何为JFrame进行键绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何为JFrame创建键绑定,而不管帧中的焦点是什么?

How do we make key bindings for a JFrame regardless of what's in focus in the frame?

我已经看过这个问题:如何为java.awt.Frame创建键绑定?

I already looked at this question: How do you make key bindings for a java.awt.Frame?

我尝试为JFrame的根窗格设置输入映射,但是当焦点位于JTextArea上时,即使editable为false,它也不起作用。

I tried setting the input map for the root pane of the JFrame, but it doesn't work when the focus is on a JTextArea even though editable is false.

使密钥绑定在整个JFrame中工作的最简单方法是什么?

What's the easiest way to make key bindings work across an entire JFrame?

推荐答案


我尝试为JFrame的根窗格设置输入映射,但是当焦点位于JTextArea上时,即使editable为false,它也不起作用。

I tried setting the input map for the root pane of the JFrame, but it doesn't work when the focus is on a JTextArea even though editable is false.

正确。如果组件具有焦点并实现相同的绑定,则该绑定将具有首选项。

Correct. If a component has focus and implements the same binding then that binding will have preference.

如果您不希望该绑定适用于文本区域,则需要从文本区域中删除绑定。

If you don't want that binding to be applicable for the text area then you need to remove the binding from the text area.

阅读如何获取的Swing教程中的部分使用键绑定可以解释您可以使用的各种InputMaps,以及有关如何删除绑定的示例。

Read the section from the Swing tutorial on How to Use Key Bindings for explanations of the various InputMaps that you can use and for an example on how to remove a binding.

这篇关于无论JComponent是什么焦点,你如何为JFrame进行键绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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