简体   繁体   English

Java:如何更改setDefaultButton()的激活事件键?

[英]Java: How to change activation event key for setDefaultButton()?

Is there a way to change the default activation event key for setDefaultButton()? 有没有办法更改setDefaultButton()的默认激活事件键? The default key is ENTER . 默认键是ENTER

This is the scenario: In login window, there's two(2) button, one is for login and the other is exit. 这是场景: 在登录窗口中,有两个(2)按钮,一个用于登录,另一个在退出。 Setting login button to setDefaultButton() is a good idea but I would like to have another activation event key which is ESCAPE to respond with exit button. 将登录按钮设置为setDefaultButton()是个好主意,但我想使用另一个激活事件密钥,即ESCAPE以响应退出按钮。

Thanks! 谢谢!

this is what you should do 这是你应该做的

InputMap inputMap = panel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "exitAction");
panel.getActionMap().put("exitAction", exitAction);

Action exitAction = new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            //what ever happen on exit goes here
        }
    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM