Java JOptionPane.showConfirmDialog 热键

发布于 2024-10-14 11:33:49 字数 374 浏览 13 评论 0原文

我正在使用 JOptionPane.showConfirmDialog 在 Java 中显示确认对话框。该对话框向用户显示是否确认。调用方式如下:

int result = JOptionPane.showConfirmDialog(sessionObjects.getActiveComponent(), 
                 "Are you sure you want to exit?", "My App", JOptionPane.YES_NO_OPTION);

问题是,这是一个简单的确认,用户是否可以按y表示是,按n表示否?目前用户必须点击按钮?

谢谢,

安德斯

I am displaying a confirmation dialog in Java using JOptionPane.showConfirmDialog. The dialog shows a Yes No confirmation to the user. This is called as follows:

int result = JOptionPane.showConfirmDialog(sessionObjects.getActiveComponent(), 
                 "Are you sure you want to exit?", "My App", JOptionPane.YES_NO_OPTION);

The question is as this is a simple confirmation, can the user press y for yes and n for no? At present the user has to click on the buttons?

Thanks,

Andez

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

再见回来 2024-10-21 11:33:49

还有另一种更简单的可能性:您可以在程序中按 alt ,然后以相同的方式释放它:

public static void pressAlt(){
    try {
        Robot r = new Robot();
        r.keyPress(KeyEvent.VK_ALT);
    } catch (AWTException ex) {
        Logger.getLogger(ManualDetection.class.getName()).log(Level.SEVERE, null, ex);
    }
}

public static void releaseAlt(){
    try {
        Robot r = new Robot();
        r.keyRelease(KeyEvent.VK_ALT);
    } catch (AWTException ex) {
        Logger.getLogger(ManualDetection.class.getName()).log(Level.SEVERE, null, ex);
    }
}

然后当您调用时:

 pressAlt();
 int confirm = JOptionPane.showConfirmDialog(... bla bla bla ... 
                                                  ,JOptionPane.YES_NO_OPTION);
 releaseAlt();

行为完全相同,如您所愿......

There is another simpler possibility: you can press alt in a program and then release it in a same way:

public static void pressAlt(){
    try {
        Robot r = new Robot();
        r.keyPress(KeyEvent.VK_ALT);
    } catch (AWTException ex) {
        Logger.getLogger(ManualDetection.class.getName()).log(Level.SEVERE, null, ex);
    }
}

public static void releaseAlt(){
    try {
        Robot r = new Robot();
        r.keyRelease(KeyEvent.VK_ALT);
    } catch (AWTException ex) {
        Logger.getLogger(ManualDetection.class.getName()).log(Level.SEVERE, null, ex);
    }
}

then when you call:

 pressAlt();
 int confirm = JOptionPane.showConfirmDialog(... bla bla bla ... 
                                                  ,JOptionPane.YES_NO_OPTION);
 releaseAlt();

the behavior is exactly the same, as you desired...

你好,陌生人 2024-10-21 11:33:49

您已经有按钮的“热键”(助记符):Alt+Y 表示“是”,Alt+N kbd> 表示“否”。

您还可以按 Tab 在它们之间切换,并按 空格 进行切换。

You already have "hotkeys" (mnemonics) for the buttons: Alt+Y for "Yes" and Alt+N for "No".

You can also hit Tab to toggle between them and Space to press.

思念绕指尖 2024-10-21 11:33:49

不,但您可以创建自己的 JDialog 来做到这一点。

No, but you can create your own JDialog that could do that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文