繁体   English   中英

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

[英]Set selected item in jradiobutton to multiple

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

我所知道的是,如果将JRadioButton添加到ButtonGroup ,则会将所选项目设置为1,但不会将倍数设置为我的目标。

这可能吗? 有什么方法/方法吗? 谢谢您的任何建议:)

创建一个JRadioButtons的数组列表。 每次用户单击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

现在,当用户单击您的按钮之一时,您的动作监听器将触发,您可以在此处检查已启用的金额

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);
            }
        }
    }
}

当已经选择按钮时,这应该禁用按钮,并且还仅允许用户选择阵列列表中的12个按钮。

JRadioButton是错误的类型,因为用户希望只选择一个。 最好将JCheckBox与类似SSCCE的自定义ActionListener 一起使用

    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);
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM