java JDialog取消按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1284328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JDialog cancel button
提问by Konrad Rudolph
How can I set a cancel button in a Swing JDialog, i.e. a button whose action is performed automatically if the user presses the “Cancel” key on the keyboard?
如何在 Swing 中设置取消按钮JDialog,即如果用户按下键盘上的“取消”键,其动作将自动执行的按钮?
The counterpart is offered for a default action via the setDefaultButtonmethod of the dialog's root pane.
通过setDefaultButton对话框根窗格的方法为默认操作提供对应项。
If that's helping, I'm searching for an analogue to the WinForms Form.CancelButtonproperty.
如果这有帮助,我正在寻找 WinFormsForm.CancelButton属性的类似物。
采纳答案by Rich Seller
I don't think this is possible with JDialog without extending it.
我认为 JDialog 不扩展它是不可能的。
You could use JOptionPane.showOptionDialog() (or possibly one of the other show methods), passing the JButtons you want to be used.
您可以使用 JOptionPane.showOptionDialog() (或可能是其他显示方法之一),传递您想要使用的 JButton。
If the options passed are components, they'll be rendered as normal, so you can do something like this:
如果传递的选项是组件,它们将正常呈现,因此您可以执行以下操作:
int optionType = JOptionPane.DEFAULT_OPTION;
int messageType = JOptionPane.PLAIN_MESSAGE; // no standard icon
JButton ok = new JButton("ok");
JButton cancel = new JButton("cancel");
//add any handlers to the buttons
...
//construct options
Object[] selValues = { ok, cancel };
//show dialog as normal, selected index will be returned.
int res = JOptionPane.showOptionDialog(null, "message",
"title", optionType, messageType, null, selValues,
selValues[0]);
回答by Jesse
The best way I can see is to add an Actionto the action map of the root pane, and link that action to the escape key using the root pane's input map.
我能看到的最好方法是Action在根窗格的动作映射中添加一个,并使用根窗格的输入映射将该动作链接到转义键。
For this, you need an Action. If your cancel button's behaviour is implemented as an action (ie. cancelButton.getAction() != null), then this will work:
为此,您需要一个Action. 如果您的取消按钮的行为被实现为一个动作(即cancelButton.getAction() != null),那么这将起作用:
getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "CANCEL");
getRootPane().getActionMap().put("CANCEL", cancelButton.getAction());
Otherwise, if the cancel button's logic is implemented via an ActionListener, you could have the actionPerformed()method of the ActionListenercall a private void onCancel()method that implements the logic, and register a "cancel" action that calls the same method.
否则,如果取消按钮的逻辑是通过实现ActionListener,你可以拥有actionPerformed()的的方法ActionListener调用一个private void onCancel()方法实现的逻辑,并注册了“取消”的行动,调用相同的方法。
getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "CANCEL");
getRootPane().getActionMap().put("CANCEL", new AbstractAction(){
@Override
public void actionPerformed(ActionEvent e)
{
onCancel();
}
});
回答by Prabu
Single line solution
单线解决方案
t.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
.put(KeyStroke.getKeyStroke("ESCAPE"), btnCancel.getAction());
where t is any component(except JButton) like JTextField in the dialog.
其中 t 是对话框中的任何组件(JButton 除外),如 JTextField。
回答by Eugene Ryzhikov
All you have to do is attach the action listener to the button and call dialog.setVisible(false)inside of it.
您所要做的就是将动作侦听器附加到按钮并在其中调用dialog.setVisible(false)。

