本文介绍了Swings setSelected() 不适用于 JRadioButtons 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
JRadioButton opt[] = new JRadioButton[4];
opt[0].setSelected(false); //not working
JRadioButton r1 = new JRadioButton();
r1.setSelected(false); //working
谁能告诉我为什么第一次调用 setSelected()
不起作用?
Can anyone tell my why the first call to setSelected()
isn't working?
推荐答案
Java Object
数组中元素的默认值为 null
.JRadioButton
数组 opt
没有实例化的元素.您需要在调用任何方法之前实例化它们:
The default value for elements in a Java Object
array is null
. The JRadioButton
array opt
has no elements instantiated. You need to to instantiate them prior to invoking any methods:
for (int i=0; i < opt.length; i++) {
opt[i] = new JRadioButton("Radio Button " + i);
}
这篇关于Swings setSelected() 不适用于 JRadioButtons 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!