说我们有类似的东西......
JRadioButton jb1 = new JRadioButton();
JRadioButton jb2 = new JRadioButton();
JRadioButton jb3 = new JRadioButton();
JRadioButton jb4 = new JRadioButton();
JRadioButton jb5 = new JRadioButton();
JRadioButton jb6 = new JRadioButton();
JRadioButton jb7 = new JRadioButton();
JRadioButton jb8 = new JRadioButton();
JRadioButton jb9 = new JRadioButton();
JRadioButton jb10 = new JRadioButton();
JRadioButton jb11 = new JRadioButton();
JRadioButton jb12 = new JRadioButton();
JRadioButton jb13 = new JRadioButton();
JRadioButton jb14 = new JRadioButton();
JRadioButton jb15 = new JRadioButton();
JRadioButton jb16 = new JRadioButton();
JRadioButton jb17 = new JRadioButton();
JRadioButton jb18 = new JRadioButton();
JRadioButton jb19 = new JRadioButton();
JRadioButton jb20 = new JRadioButton();
有没有办法使用循环添加所有这些radiobutton而不是一次一行,或者另一种更干净的方式,这只是循环草率。
应该清理一下我的意思,是否有办法编写一个循环,将所有这些按钮添加到面板?
答案 0 :(得分:1)
这应该很简单:
JRadioButton[] buttons = new JRadioButton[20];
for (i = 0; i < buttons.length; i++)
buttons[i] = new JRadioButton();
答案 1 :(得分:1)
尝试使用List并执行以下操作:
List<JRadioButton> radioButtons = ..
for (int i =0; i<....;i++) {
radioButtons.add(new JRadioButton());
}
然后您可以执行以下操作:
JRadioButton buttonAtSomeLocation = radioButtons.get(0);// by index
答案 2 :(得分:0)
您可以使用( FOR EXAMPLE )
ArrayList<JRadioButton> myList = new ArrayList<>();
for(int i = 0; i < myList.size(); i++){
myList.add(new JRadioButton());
}
for(int i = 0; i < myList.size(); i++){
myList.get(i).doSomethingInJRadioButtonMetohd.....
}