JDialog 取消按钮

发布于 2024-08-01 23:16:15 字数 180 浏览 2 评论 0原文

如何在 Swing JDialog 中设置取消按钮,即当用户按下键盘上的“取消”键时自动执行操作的按钮?

通过对话框根窗格的 setDefaultButton 方法提供对应的默认操作。

如果这有帮助,我正在寻找 WinForms Form.CancelButton 属性的类似物。

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?

The counterpart is offered for a default action via the setDefaultButton method of the dialog's root pane.

If that's helping, I'm searching for an analogue to the WinForms Form.CancelButton property.

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

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

发布评论

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

评论(4

梦行七里 2024-08-08 23:16:15

我能看到的最好方法是将 Action 添加到根窗格的操作映射,并使用根窗格的输入映射将该操作链接到转义键。

为此,您需要一个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());

否则,如果取消按钮的逻辑是通过 ActionListener 实现的,您可以让 ActionListeneractionPerformed() 方法调用实现逻辑的 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();
    }
});

The best way I can see is to add an Action to the action map of the root pane, and link that action to the escape key using the root pane's input map.

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:

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 ActionListener call a private void onCancel() method that implements the logic, and register a "cancel" action that calls the same method.

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();
    }
});
故事和酒 2024-08-08 23:16:15

单行解决方案

t.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
   .put(KeyStroke.getKeyStroke("ESCAPE"), btnCancel.getAction());

,其中 t 是对话框中的任何组件(JButton 除外),例如 JTextField。

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.

飘落散花 2024-08-08 23:16:15

我认为如果不扩展 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]);

I don't think this is possible with JDialog without extending it.

You could use JOptionPane.showOptionDialog() (or possibly one of the other show methods), passing the JButtons you want to be used.

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]);
小猫一只 2024-08-08 23:16:15

您所要做的就是将操作侦听器附加到按钮并在其中调用 dialog.setVisible(false)

All you have to do is attach the action listener to the button and call dialog.setVisible(false) inside of it.

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