Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java keylistener的问题_Java_Swing - Fatal编程技术网

Java keylistener的问题

Java keylistener的问题,java,swing,Java,Swing,嗨,我正在使用swing,在我的主框架(JFrame)中,我希望当用户按下+键时,一个窗口会显示测试。 如果我不调用新添加的JInternalFrame的show方法,我的KeyListener工作正常,但是当我调用我的JInternalFrame的show方法时,KeyListener将不再侦听 我已经尝试了很多来解决这个问题,但都是徒劳的,所以任何关于这方面的帮助都将不胜感激。 谢谢 这是我的钥匙 _mainFrameKeyListener = new KeyListener() {

嗨,我正在使用swing,在我的主框架(JFrame)中,我希望当用户按下+键时,一个窗口会显示测试。 如果我不调用新添加的JInternalFrame的show方法,我的KeyListener工作正常,但是当我调用我的JInternalFrame的show方法时,KeyListener将不再侦听

我已经尝试了很多来解决这个问题,但都是徒劳的,所以任何关于这方面的帮助都将不胜感激。 谢谢

这是我的钥匙

_mainFrameKeyListener = new KeyListener()
    {
        public void keyPressed(KeyEvent arg0) {
            // TODO Auto-generated method stub
            System.out.println("the key pressed Id is : " + arg0.getKeyCode());

            if(arg0.getKeyCode() == 107){
                test Test = new test();
                _mainDesktopPane.add(Test);
                Test.show();

            }
        }
        public void keyReleased(KeyEvent arg0) {
            // TODO Auto-generated method stub
        }

        public void keyTyped(KeyEvent arg0) {
            // TODO Auto-generated method stub
        }           
};

听起来你想要一个热键,而不是一个键侦听器来避免焦点问题

// Get the KeyStroke for our hot key

KeyStroke plus = KeyStroke.getKeyStroke(KeyEvent.VK_PLUS, 0, true);

// Get the input map for our component
// In this case we are interested in key strokes in the focussed window

InputMap inputMap = panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);

// Map the key stroke to our "action key" (see below)

inputMap.put(plus, "my_action");

// Get the action map for our component

ActionMap actionMap = panel.getActionMap();

// Add the required action listener to out action map

actionMap.put("my_action", actionListener);

听起来你想要一个热键而不是一个键侦听器来避免焦点问题

// Get the KeyStroke for our hot key

KeyStroke plus = KeyStroke.getKeyStroke(KeyEvent.VK_PLUS, 0, true);

// Get the input map for our component
// In this case we are interested in key strokes in the focussed window

InputMap inputMap = panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);

// Map the key stroke to our "action key" (see below)

inputMap.put(plus, "my_action");

// Get the action map for our component

ActionMap actionMap = panel.getActionMap();

// Add the required action listener to out action map

actionMap.put("my_action", actionListener);

您需要将密钥侦听器添加到具有焦点的组件中(许多组件实际上是复合组件)


因此,在聚焦窗口中使用条件为
JComponent.registerKeyboardAction
。或者使用
JComponent.getInputMap(当在聚焦窗口中时,为true)
JComponent.getActionMap(为true)
,如
registerKeyboardAction
API文档中所述。

您需要将密钥侦听器添加到具有焦点的组件中(许多组件实际上是复合组件)


因此,在聚焦窗口中使用条件为
JComponent.registerKeyboardAction
。或者使用
JComponent.getInputMap(当在聚焦窗口中时为true)
JComponent.getActionMap(true)
,如
registerKeyboardAction
API文档中所述。

请检查是否引发运行时异常。可能是您显示此对话框的线程错误,或者其他问题可能引发此异常


另外,请考虑使用异步线程来显示对话框,而不是使用侦听器线程。但这只是一个想法。

请检查是否引发运行时异常。可能是您显示此对话框的线程错误,或者其他问题可能引发此异常


另外,请考虑使用异步线程来显示对话框,而不是使用侦听器线程。但这只是一个想法。

test test=new test();-让我畏缩:)所以它只能工作一次,但当显示内部框架时,它会停止吗?是因为刚刚打开的内部框架可能有焦点吗?test test=new test();-让我畏缩:)所以它只能工作一次,但当显示内部框架时,它会停止吗?这是因为刚刚打开的内部框架可能有焦点吗?答案如何,而不是链接?链接包含示例代码,说明如何执行我的建议,即添加热ket而不是使用密钥侦听器不幸的是,“KeyStroke.getKeyStroke(KeyEvent.VK_PLUS,0,true)”对我不起作用。但是“KeyStroke.getKeyStroke(+')”可以很好地工作。也许这条评论对某人有帮助。一个答案,而不是一个链接怎么样?这个链接包含了一个示例代码,演示了如何执行我的建议,即添加一个热的ket而不是使用按键侦听器不幸的是,“KeyStroke.getKeyStroke(KeyEvent.VK_PLUS,0,true)”对我不起作用。但是“KeyStroke.getKeyStroke(+')”可以很好地工作。也许这个评论可以帮助别人。我没有得到你的回复!我处理的是按键事件,而不是任何多线程问题。不管怎样,问题解决了。我并没有得到你们的答复!我处理的是按键事件,而不是任何多线程问题。不管怎样,问题解决了。