秋千:拦截点击单击jradiobutton

发布于 2025-01-29 04:43:04 字数 1490 浏览 4 评论 0 原文

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

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

发布评论

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

评论(2

可爱咩 2025-02-05 04:43:04

itemListener 添加到 b 广播按钮。请参阅如何使用无线电按钮。选择更改后调用听众。

如果选择了 b 按钮,请显示 joptionpane 要求用户确认。请参阅如何进行对话 Joptionpane 也可以通过击中 esc 键或按右上角的顶部, x 按钮来关闭。

如果用户通过单击按钮来关闭 joptionpane ,我们什么也不做,因为实际上已经选择了按钮 b 。如果用户关闭 joptionpane 任何其他方式,那么我们需要将选择重置为按钮 a

import java.awt.EventQueue;
import java.awt.event.ItemEvent;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class Intercep {
    private JFrame frame;
    private JRadioButton aRadioButton;
    private JRadioButton bRadioButton;

    private void buildAndDisplayGui() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(createButtons());
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private JPanel createButtons() {
        JPanel panel = new JPanel();
        ButtonGroup bg = new ButtonGroup();
        aRadioButton = new JRadioButton("A");
        bRadioButton = new JRadioButton("B");
        bRadioButton.addItemListener(this::handleItem);
        bg.add(aRadioButton);
        bg.add(bRadioButton);
        panel.add(aRadioButton);
        panel.add(bRadioButton);
        return panel;
    }

    private void handleItem(ItemEvent event) {
        if (event.getStateChange() == ItemEvent.SELECTED) {
            int result = JOptionPane.showConfirmDialog(frame,
                                                       "Are you sure?",
                                                       "Confirm",
                                                       JOptionPane.YES_NO_OPTION);
            switch (result) {
                case 0:
                    // YES
                    break;
                case -1:
                    // <ESC> or 'X'
                case 1:
                    // NO
                default:
                    aRadioButton.setSelected(true);
            }
        }
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(() -> new Intercep().buildAndDisplayGui());
    }
}

请注意以上代码中的这一行:

bRadioButton.addItemListener(this::handleItem);

该行被称为

Add an ItemListener to the B radio button. Refer to How to Use Radio Buttons. The listener is invoked after the selection has changed.

If the B button is selected, display a JOptionPane asking the user to confirm. Refer to How to Make Dialogs. A JOptionPane can also be closed by hitting the ESC key or pressing the X button in the top, right corner.

If the user closes the JOptionPane by clicking the YES button, we do nothing since button B is actually already selected. If the user closes the JOptionPane any other way, then we need to reset the selection to button A.

import java.awt.EventQueue;
import java.awt.event.ItemEvent;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class Intercep {
    private JFrame frame;
    private JRadioButton aRadioButton;
    private JRadioButton bRadioButton;

    private void buildAndDisplayGui() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(createButtons());
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private JPanel createButtons() {
        JPanel panel = new JPanel();
        ButtonGroup bg = new ButtonGroup();
        aRadioButton = new JRadioButton("A");
        bRadioButton = new JRadioButton("B");
        bRadioButton.addItemListener(this::handleItem);
        bg.add(aRadioButton);
        bg.add(bRadioButton);
        panel.add(aRadioButton);
        panel.add(bRadioButton);
        return panel;
    }

    private void handleItem(ItemEvent event) {
        if (event.getStateChange() == ItemEvent.SELECTED) {
            int result = JOptionPane.showConfirmDialog(frame,
                                                       "Are you sure?",
                                                       "Confirm",
                                                       JOptionPane.YES_NO_OPTION);
            switch (result) {
                case 0:
                    // YES
                    break;
                case -1:
                    // <ESC> or 'X'
                case 1:
                    // NO
                default:
                    aRadioButton.setSelected(true);
            }
        }
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(() -> new Intercep().buildAndDisplayGui());
    }
}

Note this line in the above code:

bRadioButton.addItemListener(this::handleItem);

This is referred to as a method reference.

苍暮颜 2025-02-05 04:43:04

您想听听 /code> s知道何时选择第一个按钮(请参阅 this )。您可以使用 检查是否选择了另一个按钮。最后,您可以使用 joptionpane#showconfirmdialog(component,object,object,string,int) 提示用户“是”或“否,“使用 AbstractButton#SetSelected(boolean) buttongroup#clearSelection 控制选择了哪些按钮。

这应该让您沿着正确的轨道开始。

You want to listen for ItemEvents to know when the first button is selected (see this). You can use AbstractButton#isSelected to check that the other button is selected. Finally, you can use JOptionPane#showConfirmDialog(Component, Object, String, int) to prompt the user for "Yes" or "No," and use something like AbstractButton#setSelected(boolean) or ButtonGroup#clearSelection to control which buttons are selected.

This should get you started down the right track.

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