[英]Java - Key Bindings not works
I try this simple code and System.out.println
works when zoom button not pressed. 我尝试这个简单的代码,当没有按下缩放按钮时, System.out.println
工作。 But after pressing zoom button System.out.println
not works. 但按下变焦按钮后System.out.println
不起作用。 Why? 为什么?
zoomFree.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
isZoomFree = true;
chartPanel.setDomainZoomable(true);
chartPanel.setRangeZoomable(true);
zoomIn.setEnabled(false);
zoomFree.setEnabled(false);
}
});
Action escapeZoom = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("ESCAPE pressed! yahoo!");
}
};
panel.getInputMap().put(KeyStroke.getKeyStroke("ESCAPE"), "escape pressed");
panel.getActionMap().put("escape pressed", escapeZoom);
When you pass no arguments to the getInputMap
method, your key bindings will work only if the component is focused. 如果不向getInputMap
方法传递任何参数,则只有在关注组件时,键绑定才有效。
Try using getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
instead: 请尝试使用getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
:
panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ESCAPE"), "escape pressed");
panel.getActionMap().put("escape pressed", escapeZoom);
This way the component doesn't need to have the focus, they will work when the window has the focus 这样组件不需要具有焦点,它们将在窗口具有焦点时起作用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.