Java Swing键盘绑定 [英] Java swing keybinding

查看:139
本文介绍了Java Swing键盘绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在JPanel的构造函数中,但是当我按"h"时它不会打印任何内容.如果需要更多代码,我可以提供.谢谢!

This is in the constructor of a JPanel but it does not print anything when I press "h". If more code is needed, I can provide it. Thank you!

String hide = "hide";
    this.getInputMap().put(KeyStroke.getKeyStroke('h'), hide);
    this.getActionMap().put(hide, new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
                System.out.println("HIDDEN");
            if (isHidden){
                slide.setVisible(true);
            }else{
                slide.setVisible(false);
            }
        }
    });

推荐答案

this.getInputMap()....

您试图将绑定添加到默认的InputMap,即组件具有焦点时的默认InputMap.默认情况下,JPanel不具有焦点.您应该尝试使用getInputMap(int)方法来使用其他InputMap之一.否则,您将需要使面板聚焦并使其聚焦.

You are trying to add the bindings to the default InputMap, which is the InputMap when the component has focus. By default a JPanel does not have focus. You should try using one of the other InputMaps by using the getInputMap(int) method. Or you will need to make the panel focusable and give it focus.

阅读如何使用键绑定上的Swing教程,以获取有关以下内容的更多信息:用于指定所需InputMap的适当变量.

Read the Swing tutorial on How to Use Key Bindings for more information on the proper variables to use to specify the desired InputMap.

这篇关于Java Swing键盘绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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