gpt4 book ai didi

java - 2D JComboBox,其中一个使用 ActionListener 控制另一个项目

转载 作者:行者123 更新时间:2023-11-30 10:35:45 24 4
gpt4 key购买 nike

我坚持在 Java GUI 窗体中制作两个下拉菜单,第一个的选择将决定第二个菜单中的选择。

我希望达到的效果是这样的: enter image description here

我在 comboBox1 中切换选择后,它看起来像这样: enter image description here

这是我的测试代码:

    public static void main(String[] args) {
Tester tester = new Tester();
String[] flower = {"Rose", "Tulip"};
String[] color1 = {"Yellow", "Blue", "Red"};
String[] color2 = {"Purple", "White", "Green"};

for (String flowerPicked : flower) {
tester.comboBox1.addItem(flowerPicked);
}
tester.comboBox1.addActionListener(e -> {
// remove previous items in comboBox2 everytime a new item in box1 is selcted
tester.comboBox2.removeAllItems();
String flowerChoice = tester.comboBox1.getSelectedItem().toString();
if (flowerChoice.equalsIgnoreCase("Rose"))
for (String colorPicked : color1) {
tester.comboBox2.addItem(colorPicked );
}
else
for (String type : color2) {
tester.comboBox2.addItem(type);
}
});
tester.comboBox2.addActionListener(e -> {
String colorChoice = tester.comboBox2.getSelectedItem().toString();
String flowerChoice = tester.comboBox1.getSelectedItem().toString();
system.out.println(colorChoice + " " + flowerChoice);
});
}

但每次我尝试在 comboBox1 中切换我的选择时,我总是会在 removeAllItems() 和 comboBox2.getSelectedItems() 处遇到 NullPointerException。

我试图调试它,但似乎是因为每当程序执行 removeAllItems() 和 comboBox2.addItem() 时,都会调用 comboBox2 的 actionListener。我不知道如何处理这个

一点帮助?

最佳答案

你是对的,从 JComboBox 中删除所有项目会导致其 ActionListener 触发并返回 null 选择。

可能的解决方案:

  1. 在删除所有项目之前从 JComboBox 中删除所有 ActionListeners,然后在完成后替换监听器。 -- 或者 --
  2. 不要在返回的项上调用 toString()(这就是抛出 NPE 的原因——在 null 引用上调用 toString()),而是将选择的作为字符串返回的项目。类型转换不会抛出 NPE。

第一个例子:

ActionListener[] actionListeners = tester.comboBox2.getActionListeners();
for (ActionListener actionListener : actionListeners) {
tester.comboBox2.removeActionListener(actionListener);
}
tester.comboBox2.removeAllItems();
String flowerChoice = tester.comboBox1.getSelectedItem().toString();
if (flowerChoice.equalsIgnoreCase("Rose"))
for (String colorPicked : color1) {
tester.comboBox2.addItem(colorPicked);
}
else {
for (String type : color2) {
tester.comboBox2.addItem(type);
}
}
for (ActionListener actionListener : actionListeners) {
tester.comboBox2.addActionListener(actionListener);
}

第二个例子:

String colorChoice = (String) tester.comboBox2.getSelectedItem();
String flowerChoice = (String) tester.comboBox1.getSelectedItem();
System.out.println(colorChoice + " " + flowerChoice);

关于java - 2D JComboBox,其中一个使用 ActionListener 控制另一个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40955098/

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