2

我正在尝试JComboBox在 JAVA 中设计一个三级联:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class ThreeCascadeJComboBox {

private JComboBox combo1;

private JComboBox combo2;

private JComboBox combo3;

public static void main(String[] args) {
    new ThreeCascadeJComboBox();
}

public ThreeCascadeJComboBox() {
    JFrame v = new JFrame();
    v.getContentPane().setLayout(new FlowLayout());
    combo1 = new JComboBox();
    loadCombo1();
    combo1.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {
            loadCombo2((String) combo1.getSelectedItem());
        }

    });

    combo2 = new JComboBox();
    loadCombo2((String) combo1.getSelectedItem());
    combo2.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {
            loadCombo3((String) combo2.getSelectedItem());
        }

    });


    combo3 = new JComboBox();
    loadCombo3((String) combo2.getSelectedItem());

    v.getContentPane().add(combo1);
    v.getContentPane().add(combo2);
    v.getContentPane().add(combo3);
    v.pack();
    v.setVisible(true);
    v.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

private void loadCombo1() {
    combo1.addItem("letters");
    combo1.addItem("numbers");
}

private void loadCombo2(String seleccionEnCombo1) {
    combo2.removeAllItems();
    if (seleccionEnCombo1.equals("letters")) {
        combo2.addItem("A");
        combo2.addItem("B");
        combo2.addItem("C");
    } else if (seleccionEnCombo1.equals("numbers")) {
        combo2.addItem("1");
        combo2.addItem("2");
        combo2.addItem("3");
    }

}

private void loadCombo3(String seleccionEnCombo2) {
    combo3.removeAllItems();
    if (seleccionEnCombo2.equals("A")) {
        combo3.addItem("A-1");
        combo3.addItem("A-2");
        combo3.addItem("A-3");
    } else if (seleccionEnCombo2.equals("B")) {
        combo3.addItem("B-1");
        combo3.addItem("B-2");
        combo3.addItem("B-3");
    } else if (seleccionEnCombo2.equals("C")) {
        combo3.addItem("C-1");
        combo3.addItem("C-2");
        combo3.addItem("C-3");
    } else if (seleccionEnCombo2.equals("1")) {
        combo3.addItem("1-a");
        combo3.addItem("1-b");
        combo3.addItem("1-c");
    } else if (seleccionEnCombo2.equals("2")) {
        combo3.addItem("2-a");
        combo3.addItem("2-b");
        combo3.addItem("2-c");
    } else if (seleccionEnCombo2.equals("3")) {
        combo3.addItem("3-a");
        combo3.addItem("3-b");
        combo3.addItem("3-c");
    }   
}
}

但是当我选择以下numbers值时,我得到了下一个异常jcombo1

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at es.mycompany.MyView.ThreeCascadeJComboBox.loadCombo3(ThreeCascadeJComboBox.java:78)
    at es.mycompany.MyView.ThreeCascadeJComboBox.access$3(ThreeCascadeJComboBox.java:76)
    at es.mycompany.MyView.ThreeCascadeJComboBox$2.actionPerformed(ThreeCascadeJComboBox.java:40)
    at javax.swing.JComboBox.fireActionEvent(Unknown Source)
    at javax.swing.JComboBox.contentsChanged(Unknown Source)
    at javax.swing.JComboBox.intervalRemoved(Unknown Source)
    at javax.swing.AbstractListModel.fireIntervalRemoved(Unknown Source)
    at javax.swing.DefaultComboBoxModel.removeAllElements(Unknown Source)
    at javax.swing.JComboBox.removeAllItems(Unknown Source)
    at es.mycompany.MyView.ThreeCascadeJComboBox.loadCombo2(ThreeCascadeJComboBox.java:63)
    at es.mycompany.MyView.ThreeCascadeJComboBox.access$1(ThreeCascadeJComboBox.java:62)
    at es.mycompany.MyView.ThreeCascadeJComboBox$1.actionPerformed(ThreeCascadeJComboBox.java:30)
    at javax.swing.JComboBox.fireActionEvent(Unknown Source)
    at javax.swing.JComboBox.setSelectedItem(Unknown Source)
    at javax.swing.JComboBox.setSelectedIndex(Unknown Source)
    at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(Unknown Source)
    at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at javax.swing.plaf.basic.BasicComboPopup$1.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$400(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
4

4 回答 4

2

抛出异常是因为此时seleccionEnCombo2null.

您可以在中添加一个检查nullcombo2 ActionListener它会正常工作:

if (combo2.getSelectedItem() != null) {
  loadCombo3((String) combo2.getSelectedItem());
}
于 2013-02-27T19:07:37.917 回答
1

问题是ActionListenerforcombo1正在触发一个不会有任何选定项目的 for (因为它是空的)ActionEventcombo2您可以添加支票:

if (combo2.getSelectedItem() != null) {
    loadCombo3((String) combo2.getSelectedItem());
}
于 2013-02-27T19:10:54.397 回答
0

正如其他帖子所提到的,组合的选定值在某些情况下为空。发生这种情况是因为您可能没有意识到 combo2 的 ActionListener 被调用了两次。第一次被调用是在调用removeAllElements. 这就是 null 值的来源。第二次是您在代码中假设的唯一调用 - 这是对组合框的填充以及用户交互的响应。

于 2013-02-27T19:18:01.537 回答
0

当您加载第二个组合框时,会触发该框的动作事件(因为已经发生了动作[动作不限于选择]。第二个组合框的 actionPerformed 尝试根据第二个组合框的选择,并且没有。那是你的空指针,第二个组合框中不存在的选择。

于 2013-02-27T19:18:53.627 回答