注册 登录
编程论坛 JAVA论坛

JAVA中JRadioButton如何使用add

qq1274371820 发布于 2015-09-03 14:49, 1541 次点击
add(r1);这里开始出错.
     类型 Container 中的方法 add(Component)对于参数(JRadioButton)不适用
     要怎么改才可以在JRadioButton中用add呢

     JCheckBox t1=new JCheckBox("Visual Basic");
     JCheckBox t2=new JCheckBox("Visual C++");
     JCheckBox t3=new JCheckBox("Java");
     ButtonGroup bg=new ButtonGroup();
     JRadioButton r1=new JRadioButton("专科",true);
     JRadioButton r2=new JRadioButton("本科",false);
     JRadioButton r3=new JRadioButton("硕士");
     JRadioButton r4=new JRadioButton("博士",false);
     Label lb1=new Label("你的学历为:");
     Label lb2=new Label("你精通的语言为:");
     public JCheckBoxRadioButtonTest (String str){
         super(str);
         setLayout(new GridLayout(10,1));
         bg.add(r1);
         bg.add(r2);
         bg.add(r3);
         bg.add(r4);
         add(lb1);
         add(r1);
         add(r2);
         add(r3);
         add(r4);
         add(lb2);
         add(t1);
         add(t2);
         add(t3);
16 回复
#2
林月儿2015-09-04 19:02
setLayout(new GridLayout(10,1));

网格布局10行一列,恩。。。。是不是没实现容器接口
#3
qq12743718202015-09-05 10:28
回复 2楼 林月儿
-.-什么是实现容器接口


                    这是全部代码

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JCheckBoxRadioButtonTest extends JFrame{
    JCheckBox t1=new JCheckBox("Visual Basic");
    JCheckBox t2=new JCheckBox("Visual C++");
    JCheckBox t3=new JCheckBox("Java");
    ButtonGroup bg=new ButtonGroup();
    JRadioButton r1=new JRadioButton("专科",true);
    JRadioButton r2=new JRadioButton("本科",false);
    JRadioButton r3=new JRadioButton("硕士");
    JRadioButton r4=new JRadioButton("博士",false);
    Label lb1=new Label("你的学历为:");
    Label lb2=new Label("你精通的语言为:");
    public JCheckBoxRadioButtonTest (String str){
        super(str);
        setLayout(new GridLayout(10,1));
        bg.add(r1);
        bg.add(r2);
        bg.add(r3);
        bg.add(r4);
        add(lb1);
        add(r1);
        add(r2);
        add(r3);
        add(r4);
        add(lb2);
        add(t1);
        add(t2);
        add(t3);            
        addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                System.exit(0);
                System.out.println("program exited");
            }
        });
    }
    public static void main(String[] args){
        JCheckBoxRadioButtonTest jCheckBoxRadioButtonTest=new JCheckBoxRadioButtonTest("个人基础信息统计");
        jCheckBoxRadioButtonTest.setSize(400,250);
        jCheckBoxRadioButtonTest.setVisible(true);
    }
}
#4
林月儿2015-09-05 13:10
程序代码:
public class JCheckBoxRadioButtonTest extends JFrame{
    /**
     *
     
*/
    private static final long serialVersionUID = 1L;
    JCheckBox t1=new JCheckBox("Visual Basic");
    JCheckBox t2=new JCheckBox("Visual C++");
    JCheckBox t3=new JCheckBox("Java");
    ButtonGroup bg=new ButtonGroup();
    JRadioButton r1=new JRadioButton("专科",true);
    JRadioButton r2=new JRadioButton("本科",false);
    JRadioButton r3=new JRadioButton("硕士");
    JRadioButton r4=new JRadioButton("博士",false);
    Label lb1=new Label("你的学历为:");
    Label lb2=new Label("你精通的语言为:");
    public JCheckBoxRadioButtonTest (String str){
        super(str);
        setLayout(new GridLayout(10,1));
        bg.add(r1);
        bg.add(r2);
        bg.add(r3);
        bg.add(r4);
        add(lb1);
        add(r1);
        add(r2);
        add(r3);
        add(r4);
        add(lb2);
        add(t1);
        add(t2);
        add(t3);            
        addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                System.out.println("program exited");
                System.exit(0);
            }
        });
    }
    public static void main(String[] args){
        JCheckBoxRadioButtonTest jCheckBoxRadioButtonTest=new JCheckBoxRadioButtonTest("个人基础信息统计");
        jCheckBoxRadioButtonTest.setSize(400,250);
        jCheckBoxRadioButtonTest.setVisible(true);
    }
}

没错啊,不过窗体事件处理顺序调一下先打印后退出。
#5
qq12743718202015-09-05 21:29
回复 4楼 林月儿
但是会报错
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
      public class JCheckBoxRadioButtonTest extends JFrame{
     /**
      *
      */
     private static final long serialVersionUID = 1L;
     JCheckBox t1=new JCheckBox("Visual Basic");
     JCheckBox t2=new JCheckBox("Visual C++");
     JCheckBox t3=new JCheckBox("Java");
     ButtonGroup bg=new ButtonGroup();
     JRadioButton r1=new JRadioButton("专科",true);
     JRadioButton r2=new JRadioButton("本科",false);
     JRadioButton r3=new JRadioButton("硕士");
     JRadioButton r4=new JRadioButton("博士",false);
     Label lb1=new Label("你的学历为:");
     Label lb2=new Label("你精通的语言为:");
     public JCheckBoxRadioButtonTest (String str){
         super(str);
         setLayout(new GridLayout(10,1));
         bg.add(r1);
         bg.add(r2);
         bg.add(r3);
         bg.add(r4);
         add(lb1);
         add(r1);             //类型 Container 中的方法 add(Component)对于参数(JRadioButton)不适用
         add(r2);
         add(r3);
         add(r4);
         add(lb2);
         add(t1);
         add(t2);
         add(t3);            
         addWindowListener(new WindowAdapter(){
             public void windowClosing(WindowEvent e){
                 System.out.println("program exited");
                 System.exit(0);
             }
         });
     }
     public static void main(String[] args){
         JCheckBoxRadioButtonTest jCheckBoxRadioButtonTest=new JCheckBoxRadioButtonTest("个人基础信息统计");
         jCheckBoxRadioButtonTest.setSize(400,250);
         jCheckBoxRadioButtonTest.setVisible(true);
     }
 }
#6
林月儿2015-09-06 07:29
回复 5楼 qq1274371820
不会吧,又不是泛型
#7
qq12743718202015-09-06 12:00
回复 6楼 林月儿
#8
林月儿2015-09-06 14:31
回复 7楼 qq1274371820
编译器问题?helloworld能打印?
#9
qq12743718202015-09-07 17:24
回复 8楼 林月儿
我用的eclipse啊
#10
林月儿2015-09-07 17:37
回复 9楼 qq1274371820
好巧,我也是,qq是昵称?
#11
qq12743718202015-09-16 14:44
回复 10楼 林月儿
是啊,嘿嘿
#12
林月儿2015-09-16 14:47
回复 11楼 qq1274371820
程序能跑起来了吗
#13
qq12743718202015-09-27 10:33
回复 12楼 林月儿
并不能-.-
#14
林月儿2015-09-27 10:52
以下是引用qq1274371820在2015-9-27 10:33:44的发言:

并不能-.-


这么长时间过去了,早就忘了吧
#15
qq12743718202015-10-13 13:07
回复 14楼 林月儿
差不多
#16
林月儿2015-10-13 13:28
。。。
#17
qq12743718202015-11-15 13:00


       帖子可以插入图片吗0.0
1