您的位置:首页 > 其它

动态创建组件时遇到的问题及解决方法

2017-07-13 13:44 666 查看
动态创建组建时用到的变量值需在方法内定义

for (int i = 1; i <= cnt_choice; i++) {
final int index = i - 1;

mypanel = new JPanel();
ButtonGroup buttonGroup_1 = new ButtonGroup();
mypanel.setBorder(new TitledBorder(UIManager
.getBorder("TitledBorder.border"), "\u7B2C" + i
+ "\u9898\u9009\u62e9\u9898", TitledBorder.LEADING,
TitledBorder.TOP, null, new Color(51, 153, 255)));
mypanel.setBounds(10, 23 + x, 208, 50);
panel_4.setPreferredSize(new Dimension(100, 63 * cnt_choice));
x = 60 * i;
mypanel.setLayout(null);
for(int k=1;k<=4;k++){
final int y;
y=k;
String choice="";
final String temp;
switch(k){
case 1:choice="A";break;
case 2:choice="B";break;
case 3:choice="C";break;
case 4:choice="D";break;
}
final JRadioButton jbTest = new JRadioButton(choice);
buttonGroup_1.add(jbTest);
jbTest.setBounds(17+48*(k-1), 21, 46, 17);
temp=choice;
jbTest.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (jbTest.isSelected()) {
stu_ans[index] = temp;
System.out.println("第"+(index+1)+"个"+temp);
}
}
});
mypanel.add(jbTest);
}
panel_4.add(mypanel);

// thread.start();

}


例如代码中的变量y
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐