我有4个复选框,它们被组合在一起以便我只能选择其中一个。 但是如果我选择其中一个,现在想要取消所有复选框的选择,我无法做到。有人知道如何解决吗?
您所询问的行为是半复选框和半单选按钮,以下是正确的做法:将其设置为五个单选按钮组,其中第一个(或最后一个)基本上表示“没有其他选项”。
在@trashgod的答案基础上,这里提供一个可运行的代码示例,其中一个JButton
与一组JRadioButton
对象连接起来,使用ButtonGroup.clearSelection()
清除选择:
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();
}
});
}
}
JRadioButton
实例替换为JCheckBox
,此代码同样适用。然而,不建议这样做,因为标准的表示只能选择一项的方法是使用单选按钮而不是复选框。如果用户看到复选框,通常会期望允许选择多个,当选择其中一个复选框导致其他复选框取消选择时,他们会感到烦恼。JRadioButton
按钮的外观,则建议尝试更改Swing外观和感觉以改变应用程序中所有复选框和单选按钮,直到您满意为止。clearSelection()
,它“清除选择,使得ButtonGroup
中的按钮都没有被选中。”JRadioButton
可能比复选框更容易理解。--[编辑]--
我犯了一个错误,我回答这个问题时认为它是关于另一个问题的C# .NET项目。如果您愿意,可以投票关闭它,但在选择投反对票之前请阅读此说明。
--[/编辑]--
根据您之后所说的,您想要使用单选框来作为复选框(重新点击相同的单选框应取消选中状态)。为此,您可以使用radiobuttons
并添加一个名为“none”的选项,该选项将像未选择任何选项一样,或者放置一个“清除”按钮,以取消选择每个单选框。
或者,如果您真的想这样做,您可以使用CheckBox_CheckedChanged
处理程序,每次选中或取消选中复选框时都会触发它,因此您必须在处理程序内查看复选框是否被选中。处理程序内的代码应如下所示:
If (CheckBox1.Checked)
{
CheckBox2.Checked = False;
CheckBox3.Checked = False;
CheckBox4.Checked = False;
CheckBox5.Checked = False;
}
// same for other checkboxes
或者编写一个函数,它接收当前选中的复选框并取消其他复选框的选择,并将其放置在每个处理程序中,每个处理程序发送所需的复选框。
但是我强烈建议您选择我提供的第一种解决方案之一..
这是一个老问题...但我遇到了完全相同的问题,不想采用添加“清除选择按钮”的解决方案。 因此,我编写了自己的ButtonGroup来解决原始问题(可以取消选择按钮,使组中没有按钮被选中)。 它还添加了能够被java.util.Observer观察到的功能,每当选定的按钮更改时都会通知观察者。
以下是代码:
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);
}
}
RadioButton
的行为,它是一个选择组,一次只允许选择一个选项;Swing 内置了对此的支持。
编辑:我误读了问题。
要取消选择所有复选框,您可以迭代Container
的子项或更简单地使用ButtonGroup.getElements()
来枚举特定组中的按钮。