2012-07-29 174 views
0

我只希望上面的按鈕之一被默認選中 但setSelected(true)不起作用。 當我運行JRadoiButton的以下程序沒有被選擇爲什麼JRadioButton沒有被選中,即使使用setSelected(true)

import java.util.*; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 


public class RadioDemo implements ActionListener { 

String buttonName; 
JPanel radioPanel=new JPanel(); 
ButtonGroup group = new ButtonGroup(); 
Enumeration enl;   
    int result; 
ActionEvent e; 
JRadioButton birdButton[]; 
int i; 
Vector<JComponent> list; 
Vector<String> listName; 


    public RadioDemo(Vector<JComponent> list,Vector<String> listName,Enumeration en,Enumeration enl) 
{ 
    birdButton=new JRadioButton[list.size()]; 
     this.enl=enl; 
     this.list=list; 
     this.listName=listName; 

      for(i=0;i<list.size()-1;i++) 
     { 
      buttonName=(String)enl.nextElement(); 
       birdButton[i] = new JRadioButton(buttonName); 
      birdButton[i].setSelected(false); 
       birdButton[i].setActionCommand(buttonName);         
      group.add(birdButton[i]); 
      birdButton[i].addActionListener(this);     
      radioPanel.add(birdButton[i]); 
     } 

      buttonName=(String)enl.nextElement(); 
       birdButton[i] = new JRadioButton(buttonName); 
       birdButton[i].setSelected(true); 
      birdButton[i].setActionCommand(buttonName);         
      group.add(birdButton[i]); 
      birdButton[i].addActionListener(this);     

      radioPanel.add(birdButton[i]); 
         radioPanel.setLayout(new BoxLayout(radioPanel,BoxLayout.Y_AXIS)); 
           //birdButton.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); 
       result = JOptionPane.showConfirmDialog(null, radioPanel, 
              "Please choose", JOptionPane.OK_CANCEL_OPTION); 
     show(); 
    } 



    /** Listens to the radio buttons. */ 
     public void actionPerformed(ActionEvent e) 
     {  
      this.e=e; 
     } 

     public void show() 
     { 
      if (result == JOptionPane.OK_OPTION) 
      {  i=0; 
       while(!birdButton[i].isSelected()) 
       { 
        i++;  
        System.out.println(i); 
       } 
       //list.removeElementAt(i); 
       //listName.removeElementAt(i); 
      System.out.println(i); 
      System.out.println(e.getActionCommand()); 
      } 
     } 

我也嘗試birdButton [0] .setSelected(真); 無循環

+0

這將是很好,如果你可以縮小在你的問題的代碼到本質。我是否正確,基本上你問:「爲什麼JRadioButton上的setSelected方法不起作用?」 – 2012-07-29 12:42:34

+0

@ W.Goeman:是的,你絕對是寫作。確定即時發佈整個代碼 – tabish 2012-07-29 12:52:48

+4

爲了更好地提供幫助,請發佈[SSCCE](http://sscce.org/)。 – 2012-07-29 12:55:49

回答

3

你還沒有發佈如何調用你的構造函數,所以也許有什麼東西。我稍微修改了你的代碼,添加了一個main方法,它似乎工作正常。看看它吧:

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

import javax.swing.BoxLayout; 
import javax.swing.ButtonGroup; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.JRadioButton; 
import javax.swing.SwingUtilities; 

public class RadioDemo implements ActionListener { 

    String buttonName; 
    JPanel radioPanel = new JPanel(); 
    ButtonGroup group = new ButtonGroup(); 
    int result; 
    JRadioButton birdButton[]; 
    Vector<String> listName; 
    private JRadioButton selectedButton; 

    public RadioDemo(Vector<String> listName) { 
     birdButton = new JRadioButton[listName.size()]; 
     this.listName = listName; 
     int i = 0; 
     for (String buttonName : listName) { 
      birdButton[i] = new JRadioButton(buttonName); 
      if (i == 0) { 
       birdButton[i].setSelected(true); 
       selectedButton = birdButton[i]; 
      } 
      birdButton[i].setActionCommand(buttonName); 
      group.add(birdButton[i]); 
      birdButton[i].addActionListener(this); 
      radioPanel.add(birdButton[i]); 
      i++; 
     } 

     radioPanel.setLayout(new BoxLayout(radioPanel, BoxLayout.Y_AXIS)); 
     // birdButton.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); 
     result = JOptionPane.showConfirmDialog(null, radioPanel, "Please choose", JOptionPane.OK_CANCEL_OPTION); 
     show(); 
    } 

    /** Listens to the radio buttons. */ 
    @Override 
    public void actionPerformed(ActionEvent e) { 
     JRadioButton rb = (JRadioButton) e.getSource(); 
     System.err.println(rb.getText() + " is selected"); 
     selectedButton = rb; 
    } 

    public void show() { 
     if (result == JOptionPane.OK_OPTION) { 
      System.err.println(selectedButton.getText() + " is selected and approved"); 
     } 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       Vector<String> buttonNames = new Vector<String>(); 
       buttonNames.add("Show"); 
       buttonNames.add("Something"); 
       buttonNames.add("Else"); 
       buttonNames.add("Beep"); 
       new RadioDemo(buttonNames); 
      } 
     }); 
    } 
} 
+0

非常感謝你爲這個 實際上我是以匿名的方式調用構造函數,這就是爲什麼它不工作。 然後我把它存儲到它的工作參考變量 – tabish 2012-07-29 20:13:30

相關問題