如何在Swing中设置取消按钮JDialog
,即如果用户按下键盘上的"取消"键,其动作自动执行的按钮?
通过setDefaultButton
对话框根窗格的方法提供对应的默认操作.
如果这有帮助,我正在寻找WinForms Form.CancelButton
属性的模拟.
我认为如果不扩展 JDialog,这是不可能的。
您可以使用 JOptionPane.showOptionDialog() (或者可能是其他显示方法之一),传递您想要使用的 JButton。
如果传递的选项是组件,它们将正常渲染,因此您可以执行以下操作:
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]);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
12111 次 |
最近记录: |