如何在 JRadioButtons 的 ButtonGroup 中随机 select 一个按钮?

How to randomly select a button in a ButtonGroup of JRadioButtons?

我想在该面板初始化时 select 编辑一个随机单选按钮,但我不确定 how/if 我是否可以做到这一点。

有没有办法从组中获取随机按钮并select它?

import javax.swing.*;

public class RandomPanel extends JPanel
{
    private ButtonGroup buttonGroup;
    private String[] buttonText =
            {
                    "Red",
                    "Mashed Potatoes",
                    "Metal",
                    "Running",
                    "Butts",
                    "Turquoise"
            };

    public RandomPanel()
    {
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        setBorder(BorderFactory.createTitledBorder("Random Selections"));

        buttonGroup = new ButtonGroup();
        for (String text : buttonText)
        {
            JRadioButton option = new JRadioButton(text);
            add(option);
            button.add(option);
        }
    }

}

尝试使用 Randomclass .

    // Library location
    import java.util.Random;

    //Inside some method
    Random r = new Random();
    randomIndex = r.nextInt(buttonText.length());
    text = buttonText[randomIndex];

这需要根据您的实施进行安排,显示的是 'how-to' 用法。

Note: the argument to nextInt(args) is exclusive. i.e. will return 0 <= x < args

你可以做的是保留你创建的所有单选按钮的 list/array,然后使用按钮组的 setSelected() 方法设置选中的,像这样

buttonGroup.setSelected(buttonsArray[randomButtonNum].getModel(), true);

我相信您正在寻找类似于下面的解决方案。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.*;

public class RandomPanel extends JPanel
{
    private ButtonGroup buttonGroup;
    private String[] buttonText =
            {
                    "Red",
                    "Mashed Potatoes",
                    "Metal",
                    "Running",
                    "Butts",
                    "Turquoise"
            };

    private JRadioButton[] radioButton;

    Random r = new Random();

    public RandomPanel()
    {
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        setBorder(BorderFactory.createTitledBorder("Random Selections"));

        buttonGroup = new ButtonGroup();

        radioButton = new JRadioButton[buttonText.length];

        for(int rb=0; rb<buttonText.length; rb++)
        {
            radioButton[rb] = new JRadioButton(buttonText[rb]);
            add(radioButton[rb]);
            buttonGroup.add(radioButton[rb]);
        }

        JButton b = new JButton("Random");
        b.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                selectRandomButton();
            }
        });

        add(b);
    }

    public void selectRandomButton()
    {
        radioButton[r.nextInt(radioButton.length)].setSelected(true);
    }

    public static void main(String[] args)
    {
        JFrame f = new JFrame("Test Random Button");
        f.setSize(300, 300);
        f.setLocationRelativeTo(null);;
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        f.add(new RandomPanel());

        f.setVisible(true);;
    }
}

我创建了一个小方法,允许我设置任何单选组按钮。如果您不想将 if 用于任何单选按钮,则非常方便。

public void setButtonGroup(int rdValue, Enumeration elements ){
    while (elements.hasMoreElements()){
        AbstractButton button = (AbstractButton)elements.nextElement();
        if(Integer.parseInt(button.getActionCommand())==rdValue){
            button.setSelected(true);
        }
    }
}

然后

setButtonGroup(randomIndex, yourButtonGroup.getElements());