java吧 关注:1,260,056贴子:12,755,641
  • 2回复贴,共1

用for循环创建多个单选按钮并设置显示的文本 要怎么弄??

只看楼主收藏回复

////添加 单选按钮
private String[][] strrb4={{"黑","棕","红","橙","黄","绿","蓝","紫","灰","白"},{"金","银"}};
JRadioButton[] rb41_ = new JRadioButton[10];
JRadioButton[] rb42_ = new JRadioButton[10];
JRadioButton[] rb43_ = new JRadioButton[10];
JRadioButton[] rb44_ = new JRadioButton[2];
//这样的话按钮名就只需一句rb4[i]_[i]=new JRadioButton;了吗
rb4[i]_[i]=new JRadioButton;可以怎么理解呀,求大神指点,我觉都睡不好。。。
for(int i =0;i<=9;i++)//String[] s:strrb4[0] )
{
rb41_[i] = new JRadioButton(strrb4[0][i]);
rb42_[i] = new JRadioButton(strrb4[0][i]);
rb43_[i] = new JRadioButton(strrb4[0][i]);
}
for(int i =0 ;i<=1;i++)
{
rb44_[i] = new JRadioButton(strrb[1][i]);
}
/*/*/***=/*=/=*/=*/=/=*=/*=/*=/*=*=/*=/*=/*=*=*=*=*==**=*=/=/*=/=*
另外 ,用for-each可以吗:
for(String[] s:strrb4[1] )
{
int i=0;
rb44_[i] = new JRadioButton(s);
i++;
}
一个一个的创建按钮实在太麻烦了,但却搞不懂怎么循环命名。。。我好像太笨了点


1楼2017-12-15 17:54回复
    private String[][] strrb4={{"黑","棕","红","橙","黄","绿","蓝","紫","灰","白"},{"金","银"}};
    JRadioButton[] rb41_ = new JRadioButton[10];
    JRadioButton[] rb42_ = new JRadioButton[10];
    JRadioButton[] rb43_ = new JRadioButton[10];
    JRadioButton[] rb44_ = new JRadioButton[2];
    你的意思是把
    "黑","棕","红","橙","黄","绿","蓝","紫","灰","白" 放入 rb41_ , rb42_ , rb43_
    "金","银" 放入rb44_ ?


    IP属地:湖北2楼2017-12-15 18:02
    回复
      2025-05-18 06:28:41
      广告
      嗯嗯,是的,非常感谢!我见百度知道里面可以这样用
      JRadioButton[] rb41_ = new JRadioButton[10];
      然后总是报错,API里面也没有,我想起来好像就是无论什么数据都可以来声明数组。但是发现不知道是怎么用。
      声明完数组
      JRadioButton[] rb41_ = new JRadioButton[10];
      然后添加
      rb41_[i] = new JRadioButton(strrb4[0][i]);好像不行;
      还有不知道可不可以rb4[i]_[i] = new JRadioButton(strrb4[0][i]);
      新建完就是rb40_0;rb40_1, rb40_3.....
      rb41_0,rb41_1,rb41_2.....不知道这种可以一次实现不能,
      就是组件名的可变部分只能是组件名的末尾吗?


      3楼2017-12-17 15:34
      回复