简体   繁体   English

Swing:拦截单击 JRadiobutton

[英]Swing: Intercept click on a JRadiobutton

I want to intercept the click on a JRadioButton in a button group.我想拦截对按钮组中 JRadioButton 的单击。 More precise: When JRadioButton A is chosen and the user clicks on JRadioButton B, I want to show a Yes/No-option pane. More precise: When JRadioButton A is chosen and the user clicks on JRadioButton B, I want to show a Yes/No-option pane. Only when the user clicks "Yes", radio button B be will be selected.只有当用户单击“是”时,才会选择单选按钮 B。 If the user clicks on "No" nothing is supposed to change.如果用户单击“否”,则不应更改任何内容。 Is this or rather how is this possible?这是或者更确切地说这怎么可能?

Thanks for your time谢谢你的时间

You want to listen for ItemEvent s to know when the first button is selected (see this ).您想监听ItemEvent以了解何时选择了第一个按钮(请参阅this )。 You can use AbstractButton#isSelected to check that the other button is selected.您可以使用AbstractButton#isSelected检查是否选择了另一个按钮。 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.最后,您可以使用JOptionPane#showConfirmDialog(Component, Object, String, int)提示用户“是”或“否”,并使用AbstractButton#setSelected(boolean)ButtonGroup#clearSelection之类的东西来控制选择了哪些按钮.

This should get you started down the right tract.这应该让你开始正确的道路。

Add an ItemListener to the B radio button.ItemListener添加到B单选按钮。 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.如果选择了B按钮,则显示一个JOptionPane要求用户确认。 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. JOptionPane也可以通过按ESC键或按右上角的X按钮来关闭。

If the user closes the JOptionPane by clicking the YES button, we do nothing since button B is actually already selected.如果用户通过单击YES按钮关闭JOptionPane ,我们什么也不做,因为按钮B实际上已经被选中。 If the user closes the JOptionPane any other way, then we need to reset the selection to button A .如果用户以任何其他方式关闭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());
    }
}

Note this line in the above code:注意上面代码中的这一行:

bRadioButton.addItemListener(this::handleItem);

This is referred to as a method reference .这称为方法参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM