-1
JRadioButton opt[] = new JRadioButton[4];
opt[0].setSelected(false); //not working

JRadioButton r1 = new JRadioButton();
r1.setSelected(false); //working

谁能告诉我为什么第一次打电话setSelected()不起作用?

4

1 回答 1

1

ObjectJava数组中元素的默认值为null. 该JRadioButton数组opt没有实例化元素。您需要在调用任何方法之前实例化它们:

for (int i=0; i < opt.length; i++) {
   opt[i] = new JRadioButton("Radio Button " + i); 
}
于 2013-05-22T18:19:22.430 回答