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

动态创建组建时用到的变量值需在方法内定义

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值