重置 Java 中的单选按钮

Reset radio buttons in Java

我在一个按钮组中有三个单选按钮。我希望在单击 JButton 时重置单选按钮,使它们不被填充。我已经尝试了您键入 .set 时提出的合乎逻辑的建议,但所有布尔值都没有按照我的意愿进行。因此,如果您有任何建议,我们将不胜感激。谢谢!

只需使用ButtonGroup#clearSelection

例如...

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Action;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JRadioButton[] buttons;
        private ButtonGroup buttonGroup;

        public TestPane() {
            buttons = new JRadioButton[] {
                new JRadioButton("Nuclear"),
                new JRadioButton("Gas"),
                new JRadioButton("Stream"),
                new JRadioButton("Peddle"),
                new JRadioButton("Electric")
            };
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.weightx = 1;
            gbc.anchor = GridBagConstraints.WEST;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            buttonGroup = new ButtonGroup();
            for (JRadioButton btn : buttons) {
                add(btn, gbc);
                buttonGroup.add(btn);
            }

            JButton clear = new JButton("Clear");
            add(clear, gbc);

            clear.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    buttonGroup.clearSelection();
                }
            });
        }

    }

}