为JOptionPane对话框设置助记符和热键 [英] Setting Mnemonics and Hot Keys for a JOptionPane Dialog

查看:109
本文介绍了为JOptionPane对话框设置助记符和热键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以为JOptionPane对话框中的按钮分配热键和助记符?我希望能够在由JOptionPane生成的消息对话框中选择是",否"和取消",并按Y键单击是"按钮,按N键单击否"按钮并退出以激活退出按钮.同样,在带有确定"和取消"按钮的对话框中,我希望能够通过Enter和Escape激活它们.

Is it possible to assign hotkeys and mnemonics to the buttons in a JOptionPane Dialog? I'd like to be able, in a JOptionPane generated message dialog with the options Yes, No and Cancel, press Y to hit the Yes button, N to hit the No button and escape to activate the escape button. Similarly in a dialog with Okay and Cancel buttons I'd like to be able to activate them with enter and escape.

我尝试将JButtons设置为已经设置了助记符的JOptionPane的button Object数组.助记符有效,并且按钮在对话框中正确显示,但是,它们在激活时无法正常工作.最明显的是,它们不处理对话框.

I've attempted passing JButtons into the JOptionPane's button Object array with the Mnemonics set already. The mnemonics work and the buttons show up correctly in the dialogs, however, they do not act properly when they are activated. Most noticeably they do not dispose of the dialog.

向JOptionPane对话框的按钮添加热键和助记符的正确方法是什么?

What is the correct way to add hotkeys and Mnemonics to a JOptionPane Dialog's buttons?

推荐答案

您可以创建您的JOptionPane,然后循环浏览窗格的各个组件(子代等),以查看是否有任何组件是instanceof JButton,如果是这样,请检查文本,并设置适当的助记符.

You can create your JOptionPane, and then loop through the components of the pane (children etc.) checking to see if any components are instanceof JButton, and if so check the text, and set the proper mnemonic.

JOptionPane p = new JOptionPane();
Component[] c = p.getComponents();

这篇关于为JOptionPane对话框设置助记符和热键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆