javaGUI中下拉列表联动问题


首先  : 具体解释下  标题   下拉列表联动 就是将两个或两个以上的下拉列表连接(称作连接,后面就明白什么意思了)     

简单的      例如

当我们点击别的省或市时   

简而言之    就是实现下拉列表的联动   

现在

除了基本的组件以外    希望大家先去了解一下     哈希map     以及下拉列表的部分方法

可以参考  http://c.biancheng.net/view/1225.html    https://blog.csdn.net/weixin_44512194/article/details/92830703  这是下拉列表的方法   

对于哈希   只要简单了解一下就好了    就不推荐网站了

好了  下面我们直接看代码

  1 import java.awt.FlowLayout;
  2 import java.awt.event.ItemEvent;
  3 import java.awt.event.ItemListener;
  4 import java.util.HashMap;
  5 import javax.swing.JComboBox;
  6 import javax.swing.JFrame;
  7 import javax.swing.JLabel;
  8 import javax.swing.WindowConstants;
  9 
 10 public class JComboBoxExample  extends JFrame implements ItemListener {
 11 
 12 
 13   JLabel ProvinceJL;//Province label
 14   JComboBox ProvinceJCB;//Province drop-down list
 15   JLabel CityJL;//The city label
 16   JComboBox CityJCB;//City drop-down list
 17   JLabel DistrictAndCountyJL;//Area county label
 18   JComboBox DistrictAndCountyJCB;//District and county drop-down list
 19 
 20 
 21   //Set the corresponding hash
 22   HashMap cont1;
 23   HashMap cont2;
 24   //Component defined
 25 
 26   //A constructor
 27   public JComboBoxExample(){
 28     init();
 29 
 30   }
 31 
 32   private void init(){//Initialize the
 33 
 34     //All levels of the label
 35     ProvinceJL=new JLabel("省份:");
 36     CityJL=new JLabel("市:");
 37     DistrictAndCountyJL=new JLabel("区县:");
 38 
 39 
 40     // The string array of provinces is set to three provinces and each unit has three cities under its jurisdiction
 41     String [] ProOne={"安徽省","山东省","浙江省"};
 42     //Three corresponding cities
 43     String [] CityOne={"合肥市","芜湖市","安庆市"};
 44     String [] CityTwo={"青岛市","济南市","菏泽市"};
 45     String [] CityThree={"杭州市","宁波市","温州市"};
 46 
 47     //Nine corresponding districts and counties
 48     String [] DisOneOne={"包河区","长丰县","肥东县"};//安徽合肥
 49     String [] DisOneTwo={"镜湖区","无为县","弋江区"};//安徽芜湖
 50     String [] DisOneThree={"宿松县","怀宁县","太湖县"};//安徽安庆
 51     String [] DisTwoOne={"崂山区","平度区","黄岛区"};//山东青岛
 52     String [] DisTwoTwo={"长清区","槐荫区","平阴县"};//山东济南
 53     String [] DisTwoThree={"单县","牡丹区","定陶区"};//山东菏泽
 54     String [] DisThreeOne={"富阳区","西湖区","余杭区"};//浙江杭州
 55     String [] DisThreeTwo={"北仑区","奉化区","宁海县"};//浙江宁波
 56     String [] DisThreeThree={"苍南区","文成县","泰顺县"};//浙江温州
 57 
 58     //The corresponding list of districts and counties mapped to different provinces and cities
 59     cont1=new HashMap();
 60     cont1.put(ProOne[0],CityOne);
 61     cont1.put(ProOne[1],CityTwo);
 62     cont1.put(ProOne[2],CityThree);//对省绑定市
 63     //Bind county list to city
 64     cont2=new HashMap();
 65     cont2.put(CityOne[0],DisOneOne);
 66     cont2.put(CityOne[1],DisOneTwo);
 67     cont2.put(CityOne[2],DisOneThree);
 68     cont2.put(CityTwo[0],DisTwoOne);
 69     cont2.put(CityTwo[1],DisTwoTwo);
 70     cont2.put(CityTwo[2],DisTwoThree);
 71     cont2.put(CityThree[0],DisThreeOne);
 72     cont2.put(CityThree[0],DisThreeTwo);
 73     cont2.put(CityThree[0],DisThreeThree);
 74     //Binding to all levels of the district and county completion
 75     //Used for determination of subordinate drop-down lists
 76 
 77 
 78     //All levels of tag instantiation
 79     ProvinceJCB=new JComboBox(ProOne);
 80     CityJCB=new JComboBox(CityOne);
 81     DistrictAndCountyJCB=new JComboBox(DisOneOne);
 82 
 83     //Add monitor
 84     ProvinceJCB.addItemListener(this);
 85     CityJCB.addItemListener(this);
 86     DistrictAndCountyJCB.addItemListener(this);
 87 
 88     //Set the size
 89     setLayout(new FlowLayout());
 90     setSize(500,550);
 91 
 92     //add
 93     add(ProvinceJL);
 94     add(ProvinceJCB);
 95     add(CityJL);
 96     add(CityJCB);
 97     add(DistrictAndCountyJL);
 98     add(DistrictAndCountyJCB);
 99 
100     //show
101     setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
102     setVisible(true);
103     //Complete initialization
104   }
105 
106 
107   @Override//The event processing
108   public void itemStateChanged(ItemEvent e) {
109 
110     String to=e.getItem().toString();
111     if (cont1.containsKey(to)){
112       //Provinces have been selected、
113       CityJCB.removeAllItems();
114       String [] now=cont1.get(to);
115       for(int i=0;i){
116         CityJCB.addItem(now[i]);
117       }
118     }
119     else {
120       if (cont2.containsKey(e.getItem().toString())){
121         DistrictAndCountyJCB.removeAllItems();
122         String [] now=cont2.get(to);
123         for(int i=0;i){
124           DistrictAndCountyJCB.addItem(now[i]);
125         }
126       }
127     }
128   }
129 
130 
131   public static void main(String [] args){
132     new JComboBoxExample();
133 
134   }
135 }

如果你看了   上面的下拉列表的方法    相信你一定能看懂  

就不多做解释了