简体   繁体   English

使JDialog按钮响应Enter键

[英]Making a JDialog button respond to the Enter key

I have a JQueryDialog with a text field, an OK button and a cancel button. 我有一个带有文本字段的JQueryDialog,一个OK按钮和一个取消按钮。

I want to be able to hit the enter key after filling in the text fields and have it do the same action as when I click the OK button. 我希望能够在填写文本字段后点击回车键并让它执行与单击“确定”按钮时相同的操作。

在对话框中,调用getRootPane().setDefaultButton(okButton)

The code is almost correct. 代码几乎是正确的。 I would change the if comparison and use the correct method as noted below. 我会改变if比较并使用如下所述的正确方法。

if (KeyEvent.VK_ENTER == event.getKeyCode()) {
                yourButton.doClick();
            }

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

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