gpt4 book ai didi

java - For(each) 循环两次填充组合框

转载 作者:太空宇宙 更新时间:2023-11-04 12:00:13 24 4
gpt4 key购买 nike

我构建了一个组合框,它根据另一个组合框的内容动态填充,依此类推。我决定在填充目标组合框时尝试迭代源数组的内容,尽管这有点糟糕。然而,尽管这会产生组合框内容,但它们会重复。我已经单步执行了代码,并且该数组仅被迭代一次。

 private JComboBox regBuildingSelectBox;
...
String[] siteSelectStrings = {"Site", "London", "Long Island"};
JComboBox regSiteSelectBox = new JComboBox(siteSelectStrings);
regSiteSelectBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent arg0) {
getDropDownVariables gddv = new getDropDownVariables();
for(String s:
gddv.buildingSelectList
(regSiteSelectBox.getSelectedItem().toString()))
{
regBuildingSelectBox.addItem(s);
}
}
});
regSiteSelectBox.setBounds(24, 336, 282, 20);
contentPane.add(regSiteSelectBox);


regBuildingSelectBox = new JComboBox();

regBuildingSelectBox.setBounds(24, 367, 282, 20);
contentPane.add(regBuildingSelectBox);

包含数组的代码如下:

public class getDropDownVariables {

public String[] buildingSelectList(String site)
{
switch (site)
{
case "London":
return new String[] {"Building", "Harvell",
"LYNX Complex", "Caroline", "Salters"};
case "Long Island":
return new String[] {"Building", "Phillips", "Pascal"};
}
return new String[] {"Failed to populate buildings"};
}

结果:

enter image description here

最佳答案

只是一个猜测,但在调用 regBuildingSelectBox.addItem(s); 之前您可能必须先清除它,否则它只会不断添加到列表中而不是替换它。还可能有一种不同的方法来设置值与添加值。

关于java - For(each) 循环两次填充组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40976768/

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