大多数用户可能会有这么一个习惯:对于富客户端弹出的Dialog,习惯使用ESC将其关闭,而不是“叉掉它”。在Swing中,弹出窗口一般继承自JDialog类,但默认没有对ESC键事件做响应,下面这段代码可以完成这个功能:
private static final KeyStroke escapeStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0); public static final String dispatchWindowClosingActionMapKey = "com.jayxu:WINDOW_CLOSING"; // any key string you like public static void installEscapeCloseOperation(final JDialog dialog) { // any method name you like Action dispatchClosing = new AbstractAction() { public void actionPerformed(ActionEvent event) { dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING)); } }; JRootPane root = dialog.getRootPane(); root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeStroke, dispatchWindowClosingActionMapKey); root.getActionMap().put( dispatchWindowClosingActionMapKey, dispatchClosing); }
-- EOF --
除非注明(如“转载”、“[zz]”等),本博文章皆为原创内容,转载时请注明: 「转载自程序员的信仰©」
本文链接地址:如何在JDialog上注册全局ESC键事件
发表回复