在Google上查到的代码见下面的链接
[url]http://www.java2s.com/Code/Java/Swing-JFC/DefaultbuttonfordialogpressEntertoactivate.htm[/url]
可惜只有设定Enter的代码。
其实使用其他的方法同样可以实现此功能,并可以指定任意的快捷键。
/**
* 设置窗口的ESC键对应按钮--必须放在jbutton已经add了actionListener后
*
* @param pane
* @param jb
*/
public static void setEsc(RootPaneContainer jc, JButton jb) {
if (jb.getActionListeners().length == 0) {
System.out.println("ERR:COMMONUIUTIL_SETESC_JBUTTONHASNOACTIONLISTENERS");
return;
}
jc.getRootPane().registerKeyboardAction(jb.getActionListeners()[0], "command",
KeyStroke.getKeyStroke([color=red]KeyEvent.VK_ESCAPE[/color], 0),
JComponent.WHEN_IN_FOCUSED_WINDOW);
}
[url]http://www.java2s.com/Code/Java/Swing-JFC/DefaultbuttonfordialogpressEntertoactivate.htm[/url]
可惜只有设定Enter的代码。
其实使用其他的方法同样可以实现此功能,并可以指定任意的快捷键。
/**
* 设置窗口的ESC键对应按钮--必须放在jbutton已经add了actionListener后
*
* @param pane
* @param jb
*/
public static void setEsc(RootPaneContainer jc, JButton jb) {
if (jb.getActionListeners().length == 0) {
System.out.println("ERR:COMMONUIUTIL_SETESC_JBUTTONHASNOACTIONLISTENERS");
return;
}
jc.getRootPane().registerKeyboardAction(jb.getActionListeners()[0], "command",
KeyStroke.getKeyStroke([color=red]KeyEvent.VK_ESCAPE[/color], 0),
JComponent.WHEN_IN_FOCUSED_WINDOW);
}