package com.law;
import java.awt.*;
import javax.swing.*;
public class JRadioButtonTest extends JFrame
{
public JRadioButtonTest()
{
setTitle("JRadioButton组件");
Container container=getContentPane();
JRadioButton jb1=new JRadioButton("button1");
JRadioButton jb2=new JRadioButton("button2");
JRadioButton jb3=new JRadioButton("button3");
ButtonGroup group=new ButtonGroup();
group.add(jb1);
group.add(jb2);
group.add(jb3);
container.add(group); setBounds(100,100,300,300);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) 
{
new JRadioButtonTest();
}
}
ButtonGroup不能这样添加到容器么?提示这个错误,不知怎么解决。求指点。谢谢!
The method add(Component) in the type Container is not applicable for the arguments (ButtonGroup)

解决方案 »

  1.   

    import java.awt.*;
    import javax.swing.*;
    public class JRadioButtonTest extends JFrame
    {
        public JRadioButtonTest()
        {
            setTitle("JRadioButton组件");
            Container container=getContentPane();
            JRadioButton jb1=new JRadioButton("button1");
            JRadioButton jb2=new JRadioButton("button2");
            JRadioButton jb3=new JRadioButton("button3");
            ButtonGroup group=new ButtonGroup();
            group.add(jb1);
            group.add(jb2);
            group.add(jb3);
            container.setLayout(new FlowLayout());
            container.add(jb1);
            container.add(jb2);
            container.add(jb3);
    //        container.add(group);
            setBounds(100,100,300,300);
            setVisible(true);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }
        public static void main(String[] args)
        {
            new JRadioButtonTest();
        }
    }
      

  2.   

    ButtonGroup不能这样添加到容器
    你需要做的是讲ButtonGroup的每个Button加入到容器组建当中,上面用了最直观简单的方法,也可以通过ButtonGroup用枚举的方法遍历
      

  3.   

    你把JRadioButton放在ButtonGroup里面说明他们是一个组的,然后把JRadioButton一个个的放到容器中就好了。