gpt4 book ai didi

java - JCheckBox 的值重置/不变

转载 作者:行者123 更新时间:2023-11-29 09:23:14 26 4
gpt4 key购买 nike

我有几个组件,所有组件都继承了 JCheckBox 而没有覆盖它的任何内容,存储在一个 vector 中,然后遍历该 vector 并将每个组件添加到一个对话框中

CreateLists(); // initialises the checkbox vector

for(int i = 0; i < checkBoxes.size() ; i++){
myPanel.add(checkBoxes.elementAt(i));
}

其中一些复选框已被选中。

我现在的问题是:当我打开对话框并选择或取消选择任何复选框时, vector 中复选框的值不会改变。选中的保持选中状态,未选中的保持未选中状态。
我也尝试使用 JPanels getCompoents() 获取新值,但这些值也是错误的。
复选框继承者中的 ItemListener 确认确实发生了更改,但每当我尝试获取新值时,都与初始化复选框的值相同。

这是我用来跟踪更改的控制台输出:

create lists

print values:
checkBox1 = true
checkBox2 = true
checkBox3 = false

checkBox2 clicked new value = false
checkBox3 clicked new value = true

print values:
checkBox1 = true
checkBox2 = true
checkBox3 = false

最佳答案

这里是一些更多的代码和信息:

CreateList() 将观察列表与可观察的事物进行比较,相应地创建复选框(true = watched 等)并将它们添加到新的初始化 vector 中。

要读取我使用的值:

Component[] components = pnlGesamt.getComponents();
for(int i = 0; i < components.length; i++){
if(components[i] instanceof WLElementCheckBox){
WLElementCheckBox cb = (WLElementCheckBox) components[i];
System.out.println(cb.WlElement().Name() + " = " + cb.isSelected());
}
}

JCheckBox 继承者:

private WatchListElement wlElement;

public WLElementCheckBox (WatchListElement wl, boolean selected)
{
super();
WlElement(wl);
setSelected(selected);
setText(wlElement.Name());
addItemListener(new MIL());
}

public WatchListElement WlElement ()
{
return wlElement;
}

public void WlElement (WatchListElement wlElement)
{
this.wlElement = wlElement;
}

public class MIL implements ItemListener{

public void itemStateChanged(ItemEvent arg0) {
System.out.println("Ckb " + wlElement.Name() +" geklickt Item neu = " + isSelected());

}
}

关于java - JCheckBox 的值重置/不变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4756173/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com