有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何专注于BorderLayout中特定JPanel的键盘处理

在获取小部件焦点以在Java Swing BorderLayout的面板中启用键盘处理方面,我遇到了一些问题。我能够获得北面板的焦点,然后我的键盘处理程序就会启动。然而,如果我试图为布局的南面板或中心面板请求焦点,它根本不起作用。唯一似乎能够获得焦点的小组是北小组。有人知道为什么吗?这是按边界布局设计的吗

现在,我尝试从各种面板获取焦点的原因是,出于某种原因,当焦点设置为北面板时,似乎每隔一段时间键盘处理程序设置为在某些键上注册输入似乎根本不会启动。就好像他们不在那里。所以我想我会简单地使用布局中的另一个面板作为键盘输入的焦点,看看会发生什么,现在看来这个选项也不起作用

有人有什么想法吗

谢谢


共 (1) 个答案

  1. # 1 楼答案

    不是直接的回答,但我不能在评论中发布代码

    Is there a way to dump or display all the registered key bindings to validate they are not somehow getting unregistered?

    我用的是

    import java.awt.EventQueue;
    import javax.swing.InputMap;
    import javax.swing.JComponent;
    import javax.swing.JTree;
    import javax.swing.KeyStroke;
    
    public class KeyMappings {
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
    //              try {
    //                  UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    //              } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
    //                  ex.printStackTrace();
    //              }
    
                    getKeyBindingsFor(new JTree());
                }
            });
        }
    
        public static void getKeyBindingsFor(JComponent comp) {
            System.out.println("...WHEN_ANCESTOR_OF_FOCUSED_COMPONENT");
            getKeyBindingsFor(comp.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT));
            System.out.println("...WHEN_FOCUSED");
            getKeyBindingsFor(comp.getInputMap(JComponent.WHEN_FOCUSED));
            System.out.println("...WHEN_IN_FOCUSED_WINDOW");
            getKeyBindingsFor(comp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW));
        }
    
        public static void getKeyBindingsFor(InputMap map) {
            if (map != null) {
                KeyStroke[] keys = map.allKeys();
                if (keys != null) {
                    for (KeyStroke key : keys) {
                        Object value = map.get(key);
                        System.out.println(value + " = " + key);
                    }
                } else {
                    System.out.println("[none]");
                }
            }
        }
    }
    

    测试不同组件和外观的各种绑定