Java 行为与他们一次又一次看到的控件不同"? 我当然不打算提出任何这样的建议,如果我无意中这样做了,我将编辑我的答案以删除该建议。使用按钮清除单选按钮是非标准行为。事实上,清除单选按钮是非标准的,因为单选按钮的设计至少需要选择一个选项。我认为这是一个灰色区域。

Java 行为与他们一次又一次看到的控件不同"? 我当然不打算提出任何这样的建议,如果我无意中这样做了,我将编辑我的答案以删除该建议。使用按钮清除单选按钮是非标准行为。事实上,清除单选按钮是非标准的,因为单选按钮的设计至少需要选择一个选项。我认为这是一个灰色区域。,java,swing,Java,Swing,行为与他们一次又一次看到的控件不同"? 我当然不打算提出任何这样的建议,如果我无意中这样做了,我将编辑我的答案以删除该建议。使用按钮清除单选按钮是非标准行为。事实上,清除单选按钮是非标准的,因为单选按钮的设计至少需要选择一个选项。我认为这是一个灰色区域。我肯定见过单选按钮控件(包括汽车收音机,这个比喻最初来自汽车收音机!)没有按下按钮。是的,根据应用程序的不同,“未按下按钮”状态通常只允许作为初始状态或根本不允许。然而,我认为限制取决于应用程序。同时,单选按钮的重要之处在于选择一个选项会取消选


行为与他们一次又一次看到的控件不同"? 我当然不打算提出任何这样的建议,如果我无意中这样做了,我将编辑我的答案以删除该建议。使用按钮清除单选按钮是非标准行为。事实上,清除单选按钮是非标准的,因为单选按钮的设计至少需要选择一个选项。我认为这是一个灰色区域。我肯定见过单选按钮控件(包括汽车收音机,这个比喻最初来自汽车收音机!)没有按下按钮。是的,根据应用程序的不同,“未按下按钮”状态通常只允许作为初始状态或根本不允许。然而,我认为限制取决于应用程序。同时,单选按钮的重要之处在于选择一个选项会取消选择其他选项。
If (CheckBox1.Checked)
{
    CheckBox2.Checked = False;
    CheckBox3.Checked = False;
    CheckBox4.Checked = False;
    CheckBox5.Checked = False;
}
// same for other checkboxes
public class ClearableRadioButtons extends JFrame {

    public ClearableRadioButtons() {
        JPanel content = new JPanel();
        content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));

        final ButtonGroup group = new ButtonGroup();
        for (int i = 1; i < 10; i++) {
            JRadioButton nextButton = new JRadioButton("Button " + i);
            group.add(nextButton);
            content.add(nextButton);
        }
        content.add(new JButton(new AbstractAction("Clear") {
            public void actionPerformed(ActionEvent arg0) {
                // This is the one line you really care about:
                group.clearSelection();
            } 
        }));

        setLayout(new BorderLayout());
        add(content, BorderLayout.CENTER);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new ClearableRadioButtons();
            }
        });
    }
}
package net.yapbam.gui.util;

import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.io.Serializable;

import javax.swing.JToggleButton;

/**
 * This class is, like javax.swing.ButtonGroup, used to create a multiple-exclusion scope for
 * a set of buttons. Creating a set of buttons with the same <code>ButtonGroup</code> object means that
 * turning "on" one of those buttons turns off all other buttons in the group.
 * <p>
 * The main difference of this group with the swing one is that you can deselected buttons by clicking on them
 * and have a group without no selected button.<br>
 * Another difference is that this class extends the java.util.Observable class and calls its observers update method
 * when the selected button changes. 
 * </p>
 */
public class ButtonGroup extends Observable implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     *  The buttons list
     */
    private List<JToggleButton> buttons;
    /**
     * The current selection.
     */
    private JToggleButton selected;
    private ItemListener listener;

    /**
     * Constructor.
     */
    public ButtonGroup() {
        this.buttons = new ArrayList<JToggleButton>();
        this.selected = null;
        this.listener = new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                JToggleButton b = (JToggleButton) e.getItem();
                if (e.getStateChange()==ItemEvent.SELECTED) {
                    setSelected(b);
                } else {
                    if (selected==b) setSelected(null);
                }
            }
        };
    }

    /**
     * Adds a button to this group.
     * @param b the button to be added
     * @exception NullPointerException if the button is null
     */
    public void add(JToggleButton b) {
        if (b==null) throw new NullPointerException();
        buttons.add(b);
        b.addItemListener(listener);
        if (b.isSelected()) setSelected(b);
    }

    /**
     * Removes a button from this group.
     * @param b the button to be removed
     * @exception IllegalArgumentException if the button is unknown in this group
     * @exception NullPointerException if the button is null
     */
    public void remove(JToggleButton b) {
        if (b == null) throw new NullPointerException();
        if (this.buttons.remove(b)) {
            b.removeItemListener(this.listener);
            if (this.selected==b) setSelected(null);
        } else {
            throw new IllegalArgumentException();
        }
    }

    /**
     * Clears the selection such that none of the buttons in the
     * <code>ButtonGroup</code> are selected.
     */
    public void clearSelection() {
        if (selected != null) setSelected(null);
    }

    /**
     * Returns the selected button.
     * @return the selected button
     */
    public JToggleButton getSelected() {
        return this.selected;
    }

    /** Changes the selected button.
     * @param b the button to be selected (null deselects all buttons)
     * @exception IllegalArgumentException if the button is not in this group
     */
    public void setSelected(JToggleButton b) {
        if (b==this.selected) return;
        if ((b!=null) && (!this.buttons.contains(b))) throw new IllegalArgumentException();
        JToggleButton old = this.selected;
        this.selected = b;
        if (b!=null) b.setSelected(true);
        if (old!=null) old.setSelected(false);
        this.setChanged();
        this.notifyObservers(this.selected);
    }
}