问题
We were told to have 10 checkboxes and the user is only allowed to check 1-3 check boxes. After the user clicks a button indicating that he/she has completed the requirements and after the computer randomly selects a checkbox. The remaining checkbox wil be delared as a winner. This is a gaming program by the way.
I'm using Swing and AWT.
回答1:
You can limit a checkbox being selectable or not by disabling the component:
JCheckBox checkBox = new JCheckBox("Try to click on me");
checkBox.setEnabled(false);
If a checkbox is not enabled (disabled), the user cannot select it.
回答2:
Prevent the user from selecting additional check boxes by keeping track of each check box that is currently selected:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class CheckBoxGroup
{
private Set<GroupButtonModel> models = new HashSet<GroupButtonModel>();
private int groupSize;
public CheckBoxGroup(int groupSize)
{
this.groupSize = groupSize;
}
public void register(JCheckBox checkBox)
{
ButtonModel groupModel = new GroupButtonModel();
groupModel.setSelected ( checkBox.getModel().isSelected() );
checkBox.setModel( groupModel );
}
private class GroupButtonModel extends JToggleButton.ToggleButtonModel
{
@Override
public void setSelected(boolean selected)
{
if (!selected)
{
models.remove( this );
super.setSelected( selected );
return;
}
// Check number of currently selected check boxes
if (models.size() == groupSize)
{
System.out.println("Only " + groupSize + " items can be selected");
}
else
{
models.add( this );
super.setSelected( selected );
}
}
}
private static void createAndShowGUI()
{
JPanel panel = new JPanel();
CheckBoxGroup group = new CheckBoxGroup(3);
for (int i = 0; i < 10; i++)
{
JCheckBox checkBox = new JCheckBox( String.valueOf(i) );
panel.add( checkBox );
group.register( checkBox );
}
JFrame frame = new JFrame("Check Box Group");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( panel );
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
回答3:
If I have understood well, you want to limit the number of checkboxes the user can click during his turn. You can have a counter that tells you the number of checkboxes the user has clicked so far. When the counter reaches 1, the button will activate and the user will be able to click it. When the counter reaches 3, all the other checkboxes will deactivate. If the user wants to change the checkboxes he has selected, then he will have to uncheck any of the already checked ones.
The code will be something like this:
onCheckboxSelected()
{
if(numberOfCheckedButtons == 1)
{
activateButton();
}
else if(numberOfCheckedButtons == 3):
{
deactivateAllCheckBoxes();
}
}
onCheckboxUnselected()
{
if(numberOfCheckedButtons == 0)
{
deactivateButton();
}
else if (numberOfCheckedButtons == 2) //This is the case when the user passes from 3 selected checkboxes to 2
{
activateAllCheckboxes();
}
}
来源:https://stackoverflow.com/questions/26156310/how-can-i-limit-the-check-box-selected