用Java重置单选按钮

时间:2016-04-12 03:33:04

标签: java swing actionlistener jradiobutton

我在一个按钮组中有三个单选按钮。我希望在JButton点击时,重置单选按钮,以便它们未填充。我已经尝试过你输入.set所提出的逻辑建议,并且所有的布尔没有做我想做的事情。所以,如果你有建议,将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:2)

只需使用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();
                }
            });
        }

    }

}