设置 JOptionPane 对话框的助记符和热键
是否可以为 JOptionPane 对话框中的按钮分配热键和助记符?我希望能够在 JOptionPane 生成的带有“是”、“否”和“取消”选项的消息对话框中,按 Y 键单击“是”按钮,按 N 键单击“否”按钮,然后按 escape 键激活转义按钮。同样,在带有“确定”和“取消”按钮的对话框中,我希望能够通过输入和转义来激活它们。
我已尝试将 JButtons 传递到 JOptionPane 的按钮对象数组中,并已设置助记符。助记符有效并且按钮在对话框中正确显示,但是,它们在激活时无法正常工作。最值得注意的是,他们不会处理对话框。
将热键和助记符添加到 JOptionPane 对话框按钮的正确方法是什么?
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.
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.
What is the correct way to add hotkeys and Mnemonics to a JOptionPane Dialog's buttons?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以创建
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 areinstanceof JButton
, and if so check the text, and set the proper mnemonic.使用 UIManager 如下:
Make use of UIManager as follows:
您可以通过 Swing Worker 寻找在父窗口中打开的新窗口。然后检查它是否是一个JDialog并提取按钮区域。然后将按键分配给按钮。这适用于 JOptionPane 的静态方法。
这是 Swing Worker 类 - 只需实例化它并在调用之前执行它以显示 JOptionPane:
这是两个帮助器类,可以使事情变得更清晰:
这
就是您将如何使用它:
这是一个工作示例:
这适用于所有 JOptionPane 静态方法。只要您不是在父组件的窗口中随机弹出窗口,这就可以正常工作。注意:JOptionPane 的父组件不能为 null。
当然,实例化 JOptionPane 并自定义它可能更容易。以下是执行此操作的类:
并且
这就是您将如何使用它们:
“就数学定律而言,它们涉及现实,它们是不确定的,而就数学定律而言,它们是确定的,它们并不涉及现实。 ”
阿尔伯特·爱因斯坦
You can look for a new window opening in the parent window by means of a Swing Worker. Then check if it is a JDialog and extract the button area. Then assign keys to the buttons. This works for the static methods of JOptionPane.
This is the Swing Worker class - just instantiate it and execute it right before the call to show the JOptionPane:
These are the two helper classes to make things a little cleaner:
and
This is how you would use it:
Here is a working example:
This will work for all JOptionPane static methods. This will work fine as long as your not randomly popping up windows in the parent component's window. Note: the parent component of the JOptionPane cannot be null.
Of course, it is probably easier just to instantiate a JOptionPane and customize it. Here are the classes to do that:
and
And this is how you would use them:
"As far as the laws of mathematics refer to reality, they are not certain, and as far as they are certain, they do not refer to reality."
Albert Einstein
将按钮作为参数而不是字符串发送
Send the buttons in as parameters instead of Strings