我写了这个java程序,想实现多层下拉菜单,逐层选择的功能,目前只实现了两层,怎么改的实现多层(需要六七层吧)逐层选择呢?嘿嘿,谁有空帮我看看呢?
程序如下:import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;public class NewJFrame extends javax.swing.JFrame {
        private JComboBox jComboBox1;
        private JComboBox jComboBox2;
        private JComboBox jComboBox3;
        private static String[] diyiji={"","A","B"};
        private static String[] dierji1={"Aa","Ab"};
        private static String[] dierji2={"Ba","Bb"};
        private static String[] disanji11={"Aa1","Aa2"};
        private static String[] disanji12={"Ab1","Ab2"};
        private static String[] disanji121={"Ba1","Ba2"};
        private static String[] disanji122={"Bb1","Bb2"};        /**
        * Auto-generated main method to display this JFrame
        */
        public static void main(String[] args) {                SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                                NewJFrame inst = new NewJFrame();
                                inst.setLocationRelativeTo(null);
                                inst.setVisible(true);
                        }
                });
        }
        
        public NewJFrame() {
                super();
                initGUI();
        }
        
        private void initGUI() {
                try {
                        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
                        getContentPane().setLayout(null);
                        {                                jComboBox1 = new JComboBox(diyiji);
                                getContentPane().add(jComboBox1);
                                                                jComboBox1.setBounds(54, 61, 325, 27);
                                
                                jComboBox1.addItemListener(new ItemListener() {
                                        public void itemStateChanged(ItemEvent evt) {
                                                System.out.println("jComboBox1.itemStateChanged, event="+evt);
                                                //TODO add your code for jComboBox1.itemStateChanged
//                                                System.out.println(jComboBox1.getSelectedItem());
                                                jComboBox2.removeAllItems();
                                                if (jComboBox1.getSelectedItem().equals(diyiji[0])){
                                                        
                                                        jComboBox2.addItem("");
                                                
                                                }
                                                else if (jComboBox1.getSelectedItem().equals(diyiji[1])){
                                                        jComboBox2.removeAllItems();
                                                        for(int j=0; j<dierji1.length;j++){
                                                                jComboBox2.addItem(dierji1[j]);
//                                                                if (jComboBox2.getSelectedItem().equals(dierji1[0])){
//                                                                        jComboBox3.removeAllItems();
//                                                                        for(int v=0; v<disanji11.length;v++){
//                                                                                jComboBox3.addItem(disanji11[v]);
//                                                                        }
//                                                        }
//                                                                else if(jComboBox2.getSelectedItem().equals(dierji1[1])){
//                                                                        jComboBox3.removeAllItems();
//                                                                        for(int v=0; v<disanji11.length;v++){
//                                                                                jComboBox3.addItem(disanji12[v]);
//                                                                        }
//                                                        }
                                                                }
                                                }
                                                else if (jComboBox1.getSelectedItem().equals(diyiji[2])){
                                                        jComboBox2.removeAllItems();
                                                        for(int j=0; j<dierji1.length;j++){
                                                                jComboBox2.addItem(dierji2[j]);
                                                        }
                                                }
                                                
                                                
                                        }
                                });
                        }
                        {                                jComboBox2 = new JComboBox();
                                getContentPane().add(jComboBox2);
                                jComboBox2.setBounds(54, 161, 325, 27);
                                jComboBox2.addItemListener(new ItemListener() {
                                        public void itemStateChanged(ItemEvent evt) {
                                                System.out.println("jComboBox2.itemStateChanged, event="+evt);
//                                                //TODO add your code for jComboBox2.itemStateChanged
                                                //去掉if则第二级能够正常显示
                                                if (jComboBox2.getSelectedItem().equals(dierji1[0])){
                                                        jComboBox3.removeAllItems();
                                                        for(int v=0; v<disanji11.length;v++){
                                                                jComboBox3.addItem(disanji11[v]);
                                                        }
                                                }
                                        }
                                        /*
                                                else if (jComboBox2.getSelectedItem().equals(dierji1[1])){
                                                        jComboBox3.removeAllItems();
                                                        for(int v=0; v<disanji11.length;v++){
                                                                jComboBox3.addItem(disanji12[v]);
                                                        }
                                                }
                                                
                                        }
                                        *
                                        */
                                });
                                
                        }
                        {                                jComboBox3 = new JComboBox();
                                getContentPane().add(jComboBox3);                                jComboBox3.setBounds(54, 249, 325, 27);
                        }
                        pack();
                        setSize(500, 400);
                } catch (Exception e) {
                    //add your error handling code here
                        e.printStackTrace();
                }
        }}

解决方案 »

  1.   

    说明下:现在做了个三级的,但是第二级和第三级调试一直有问题,不是第二级不显示就是第三级不显示,。。程序中有注释已经加红色,,,恳请高手帮忙看看。。
      

  2.   

    并且用的方法超笨还希望有比较简单快捷的方法为妙哈哈
      

  3.   

    实现大概跟选择
    省,市,县,乡,村
    差不多的功能。。目前才做了三层需要有五六层。。嘿嘿