简体   繁体   English

按esc键时如何停止关闭JDialog

[英]How to stop to close the JDialog when esc key is press

how i can stop JDialog to close to close when the Esc key is press. 当Esc键被按下时,我怎么能阻止JDialog关闭。 i want keep it as it is. 我希望保持原样。 i have already tried the dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); 我已经尝试过dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); but this is not working on Esc key 但这不适用于Esc键

I was not aware that the escape key would close a dialog automatically. 我不知道转义键会自动关闭对话框。 At least it doesn't work using JDK7 on Windows 7. 至少它在Windows 7上使用JDK7不起作用。

If it does work on other Look and Feels, then I would guess that LAF is using Key Bindings. 如果它适用于其他外观和感觉,那么我猜想LAF正在使用Key Bindings。

You could try to use a dummy key binding: 您可以尝试使用虚拟键绑定:

KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
InputMap im = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
im.put(escapeKeyStroke, "do nothing");

If that doesn't help then post your SSCCE that demonstrates the problem. 如果这没有帮助,那么发布证明问题的SSCCE

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

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