2016-05-21 10 views
0

このパネルが初期化されるたびにランダムラジオボタンを選択したいのですが、どうすればいいのかわかりません。JRadioButtonsのButtonGroup内のボタンをランダムに選択する方法は?

グループからランダムなボタンを取得して選択する方法はありますか?

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

} 

答えて

2

を返しますと、作成したすべてのラジオボタンのリスト/配列を維持し、その後、ボタングループのするsetSelected()メソッドを使用して選択し、この

のようなものを設定しています
buttonGroup.setSelected(buttonsArray[randomButtonNum].getModel(), true); 
+0

にアクセスするのはええ、そうするのが最も簡単な方法です。ありがとう! – Darakath

1

Randomクラスを使用してください。

// Library location 
    import java.util.Random; 

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

これは、実装方法に合わせて配置する必要があります。使用方法は「使用方法」です。

注:nextInt(args)への引数は排他的です。つまり、あなたが何ができるか 0 <= x < args

+0

はどのようにこれは、上記のコードで実装されるだろうか? 私がボタンを選択する方法を知っている唯一の方法は、setSelectedを使用することです。そのメソッドにはパラメータとしてButtonModelが必要ですが、取得方法はわかりません。 – Darakath

+0

私の答えはあなたが今尋ねたことから少し誤っていると思います...しかし、すべての可能なボタンを保持しているインデックス可能な変数(配列など)を持っている場合は、上記のコードを使用してランダムインデックスを選択し、変数 –

0

私はあなたが以下の解決策のようなものを探していると信じています。

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

ラジオボタンを設定できる小さな方法を作成しました。ラジオボタンを使用したくない場合は非常に便利です。

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()); 
関連する問題