Java 将jradiobutton中的选定项设置为多个

Java 将jradiobutton中的选定项设置为多个,java,swing,oop,jradiobutton,Java,Swing,Oop,Jradiobutton,我有一个JRadioButton,我必须将所选项目设置为12,然后,如果已经设置为12,它将禁用/灰显其他选择 我所知道的是,如果将JRadioButton添加到ButtonGroup中,则会将所选项目设置为1,而不是我想要的多个 这可能吗?有什么方法/方法可以做到这一点吗?感谢您的任何建议:创建JRadioButtons的arraylist。每次用户单击启用的JRadioButton时,查看列表并计算已启用的JRadioButton数。如果计数大于或等于12,则禁用所有其他单选按钮,直到用户取

我有一个JRadioButton,我必须将所选项目设置为12,然后,如果已经设置为12,它将禁用/灰显其他选择

我所知道的是,如果将JRadioButton添加到ButtonGroup中,则会将所选项目设置为1,而不是我想要的多个


这可能吗?有什么方法/方法可以做到这一点吗?感谢您的任何建议:

创建JRadioButtons的arraylist。每次用户单击启用的JRadioButton时,查看列表并计算已启用的JRadioButton数。如果计数大于或等于12,则禁用所有其他单选按钮,直到用户取消选择一个为止

这只是众多方法之一

希望这有帮助

//initiate jradiobutton arraylist (this will be a field at top of class)
buttons = new ArrayList<JRadioButtons>();

//Create buttons with a listener attached
JRadioButton b1 = new JRadioButton("RadioButton One");
b1.setActionListener(myActionListener);
b1.setActionCommand("select");
buttons.add(b1);

//Add rest of buttons in the same way
JRadioButton b2...

//Add the radio buttons to your panel and such
这将在已选择按钮时禁用按钮,并且只允许用户选择arraylist中的12个按钮。

JRadioButton类型错误,因为用户希望只选择一个按钮。 最好将JCheckBox与自定义ActionListener一起使用,如下所示:


添加到多个按钮组Better use复选框。您能再解释一下,将选定的项目添加到12是什么意思吗?@Blip hello:您能将我链接到任何可以教我如何在jcheckbox中执行此操作的网站吗?非常感谢:@KyalBond,比如我有15个jradiobutton。我只希望用户从15个选项中只选择12个,然后剩下的3个选项将灰显/不可用。如果您在评论中回答您的问题,您可以使用JCheckBoxoh执行您在回答中所写的相同操作。如果我问得太多,我真的很抱歉,但您能给我一点代码吗?我是一个使用swing的初学者,我所知道的都是最基本的。非常感谢:没问题,还有很多其他的事情要考虑,但这是我所能做到的最简单的事情。@KyalBond:你的解决方案的缺点是,除非用户单击按钮,否则不会看到按钮被禁用。
public void actionPerformed(ActionEvent e){
    //Check if action was a jradiobutton
    if(e.getActionCommand().equals("select")){

        int count = 0;
        //Here check the amount of buttons selected
        for(JRadioButton button: buttons){
            if(button.isSelected()) count++;
        }

        //Now check if count is over 12
        if(count > 12){
            for(JRadioButton button: buttons){
                 //if the button trying to activate when 12 already have been, disable it
                 if(button.equals(e.getSource()) button.setSelcted(false);
            }
        }
    }
}
    public class CheckBoxActivationTest {
        public static void main(String[] args) {

            final int MAX_ACTIVE_CHECK_BOXES = 12;
            List<JCheckBox> allCheckBoxes = new ArrayList<>();

            ActionListener actionListener = new ActionListener() {
                private int activeCheckBoxesCounter = 0;
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("action!");
                    JCheckBox currentCheckBox = (JCheckBox) e.getSource();
                    activeCheckBoxesCounter += currentCheckBox.isSelected() ? 1 : -1;
                    for (JCheckBox jCheckBox : allCheckBoxes) {
                        jCheckBox.setEnabled(jCheckBox.isSelected()
                                || MAX_ACTIVE_CHECK_BOXES > activeCheckBoxesCounter);
                    }
                }
            };

            JPanel jPanel = new JPanel(new GridLayout(6, 0));
            for (int i = 0; i < 30; i++) {
                JCheckBox checkBox = new JCheckBox("Option "+(1+ i));
                allCheckBoxes.add(checkBox);
                checkBox.addActionListener(actionListener);
                jPanel.add(checkBox);
            }
            JOptionPane.showMessageDialog(null, jPanel);
        }
    }