RT

解决方案 »

  1.   

    很简单
          JRadioButton radio = new JRadioButton()
          String radioText = radio.getText();
         System.out.println(radioText);
      

  2.   

    JRadioButton radio = new JRadioButton()
    if(radop.isSelected()){
         String radioText = radio.getText();
         System.out.println(radioText);
    }
      

  3.   

    只能用if语句判断是否被选中了吗?如果有很多JRadioButton岂不是写很多if...else?有没有更简单的办法
      

  4.   

    首先将所有的JRadioButton放置到一个group中。
    然后给每个radioButton上添加一个actionListener。使用一个变量存放选中的text。
    当点击哪个radioButton。就将它的text赋给哪个变量就行了。
      

  5.   


    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.ButtonGroup;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.SwingUtilities;public class Demo{
    private JPanel jPanel;
    private JRadioButton  ja;
    private JRadioButton  jb;
    private JRadioButton  jc;
    private JRadioButton  jd;
    private ButtonGroup buttonGroup= new ButtonGroup();
    private String saveValue=null;
    private RadioButtonListener radioButtonListener=new RadioButtonListener();
    public  Demo() {
    JFrame jf=new JFrame();
    ja=new JRadioButton("A");
    ja.addActionListener(radioButtonListener);
        jb=new JRadioButton("B");
        jb.addActionListener(radioButtonListener);
        jc=new JRadioButton("C");
        jc.addActionListener(radioButtonListener);
        jd=new JRadioButton("D");
        jd.addActionListener(radioButtonListener);
        buttonGroup.add(ja);
        buttonGroup.add(jb);
        buttonGroup.add(jc);
        buttonGroup.add(jd);
        jPanel=new  JPanel();
        jPanel.add(ja);
        jPanel.add(jb);
        jPanel.add(jc);
        jPanel.add(jd);
        jf.add(jPanel);
    jf.setVisible(true);
    jf.setSize(363,213);
    }
        
    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable(){
                @Override
                public void run() {
                    new Demo();
                }
            
        });
    }

    public class RadioButtonListener implements ActionListener
    {
    @Override
    public void actionPerformed(ActionEvent arg0) {
        JRadioButton temp=(JRadioButton)arg0.getSource();
        if(temp.isSelected()){
            saveValue=temp.getText();
            System.out.println(temp.getText());
        }

    }

    }
    }