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

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

问题描述

无论框架中的焦点是什么,我们如何为 JFrame 进行键绑定?

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

我已经看过这个问题:如何为 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 教程中关于 如何使用键绑定 用于解释您可以使用的各种 InputMap 以及有关如何删除绑定的示例.

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天全站免登陆