我正在尝试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)