码迷,mamicode.com
首页 > 编程语言 > 详细

Java-Map映射集合实现省市级联选择框

时间:2018-01-17 01:23:58      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:you   []   tle   out   外观   上海   exception   round   jlist   

BackgroundPanel

 1 package com.lzw;
 2 
 3 import java.awt.Graphics;
 4 import java.awt.Image;
 5 
 6 import javax.swing.JPanel;
 7 
 8 /**
 9  * 带背景的面板组件
10  * 
11  * @author ZhongWei Lee
12  */
13 public class BackgroundPanel extends JPanel {
14 
15     /**
16      * 背景图片
17      */
18     private Image image;
19 
20     /**
21      * 构造方法
22      */
23     public BackgroundPanel() {
24         super();
25         setOpaque(false);
26         setLayout(null);
27     }
28 
29     /**
30      * 设置图片的方法
31      */
32     public void setImage(Image image) {
33         this.image = image;
34     }
35 
36     @Override
37     protected void paintComponent(Graphics g) {// 重写绘制组件外观
38         if (image != null) {
39             int width = getWidth();// 获取组件大小
40             int height = getHeight();
41             g.drawImage(image, 0, 0, width, height, this);// 绘制图片与组件大小相同
42         }
43         super.paintComponent(g);// 执行超类方法
44     }
45 }        

CityMap

 1 package com.lzw;
 2 
 3 import java.util.LinkedHashMap;
 4 import java.util.Map;
 5 
 6 public class CityMap {
 7     /**
 8      * 全国(省,直辖市,自治区)映射集合
 9      */
10     public static Map<String,String[]> model=new LinkedHashMap();
11     static{
12         model.put("北京", new String[]{"北京"});
13         model.put("上海", new String[]{"上海"});
14         model.put("天津", new String[]{"天津"});
15         model.put("重庆", new String[]{"重庆"});
16         model.put("黑龙江", new String[]{"哈尔滨","齐齐哈尔","牡丹江","大庆","伊春","双鸭山","鹤岗","鸡西","佳木斯","七台河","黑河","绥化","大兴安岭"});
17         model.put("吉林", new String[]{"长春","延边","吉林","白山","白城","四平","松原","辽源","大安","通化"});
18         model.put("辽宁", new String[]{"沈阳","大连","葫芦岛","旅顺","本溪","抚顺","铁岭","辽阳","营口","阜新","朝阳","锦州","丹东","鞍山"});
19         model.put("内蒙古", new String[]{"呼和浩特","呼伦贝尔","锡林浩特","包头","赤峰","海拉尔","乌海","鄂尔多斯","通辽"});
20         model.put("河北", new String[]{"石家庄","唐山","张家口","廊坊","邢台","邯郸","沧州","衡水","承德","保定","秦皇岛"});
21         model.put("河南", new String[]{"郑州","开封","洛阳","平顶山","焦作","鹤壁","新乡","安阳","濮阳","许昌","漯河","三门峡","南阳","商丘","信阳","周口","驻马店"});
22         model.put("山东", new String[]{"济南","青岛","淄博","威海","曲阜","临沂","烟台","枣庄","聊城","济宁","菏泽","泰安","日照","东营","德州","滨州","莱芜","潍坊"});
23         model.put("山西", new String[]{"太原","阳泉","晋城","晋中","临汾","运城","长治","朔州","忻州","大同","吕梁"});
24         model.put("江苏", new String[]{"南京","苏州","昆山","南通","太仓","吴县","徐州","宜兴","镇江","淮安","常熟","盐城","泰州","无锡","连云港","扬州","常州","宿迁"});
25         model.put("安徽", new String[]{"合肥","巢湖","蚌埠","安庆","六安","滁州","马鞍山","阜阳","宣城","铜陵","淮北","芜湖","毫州","宿州","淮南","池州"});
26         model.put("陕西", new String[]{"西安","韩城","安康","汉中","宝鸡","咸阳","榆林","渭南","商洛","铜川","延安"});
27         model.put("宁夏", new String[]{"银川","固原","中卫","石嘴山","吴忠"});
28         model.put("甘肃", new String[]{"兰州","白银","庆阳","酒泉","天水","武威","张掖","甘南","临夏","平凉","定西","金昌"});
29         model.put("青海", new String[]{"西宁","海北","海西","黄南","果洛","玉树","海东","海南"});
30         model.put("湖北", new String[]{"武汉","宜昌","黄冈","恩施","荆州","神农架","十堰","咸宁","襄樊","孝感","随州","黄石","荆门","鄂州"});
31         model.put("湖南", new String[]{"长沙","邵阳","常德","郴州","吉首","株洲","娄底","湘潭","益阳","永州","岳阳","衡阳","怀化","韶山","张家界"});
32         model.put("浙江", new String[]{"杭州","湖州","金华","宁波","丽水","绍兴","雁荡山","衢州","嘉兴","台州","舟山","温州"});
33         model.put("江西", new String[]{"南昌","萍乡","九江","上饶","抚州","吉安","鹰潭","宜春","新余","景德镇","赣州"});
34         model.put("福建", new String[]{"福州","厦门","龙岩","南平","宁德","莆田","泉州","三明","漳州"});
35         model.put("贵州", new String[]{"贵阳","安顺","赤水","遵义","铜仁","六盘水","毕节","凯里","都匀"});
36         model.put("四川", new String[]{"成都","泸州","内江","凉山","阿坝","巴中","广元","乐山","绵阳","德阳","攀枝花","雅安","宜宾","自贡","甘孜州","达州","资阳","广安","遂宁","眉山","南充"});
37         model.put("广东", new String[]{"广州","深圳","潮州","韶关","湛江","惠州","清远","东莞","江门","茂名","肇庆","汕尾","河源","揭阳","梅州","中山","德庆","阳江","云浮","珠海","汕头","佛山"});
38         model.put("广西", new String[]{"南宁","桂林","阳朔","柳州","梧州","玉林","桂平","贺州","钦州","贵港","防城港","百色","北海","河池","来宾","崇左"});
39         model.put("云南", new String[]{"昆明","保山","楚雄","德宏","红河","临沧","怒江","曲靖","思茅","文山","玉溪","昭通","丽江","大理"});
40         model.put("海南", new String[]{"海口","三亚","儋州","琼山","通什","文昌"});
41         model.put("新疆", new String[]{"乌鲁木齐","阿勒泰","阿克苏","昌吉","哈密","和田","喀什","克拉玛依","石河子","塔城","库尔勒","吐鲁番","伊宁"});
42     }
43 }

MainFrame

  1 package com.lzw;
  2 
  3 import java.awt.Color;
  4 import java.awt.EventQueue;
  5 import java.awt.event.ItemEvent;
  6 import java.awt.event.ItemListener;
  7 import java.math.BigDecimal;
  8 import java.util.Iterator;
  9 import java.util.Map;
 10 import java.util.Set;
 11 
 12 import javax.swing.DefaultComboBoxModel;
 13 import javax.swing.JButton;
 14 import javax.swing.JComboBox;
 15 
 16 import javax.swing.JFrame;
 17 import javax.swing.JLabel;
 18 import javax.swing.JList;
 19 import javax.swing.JPanel;
 20 import javax.swing.JRadioButton;
 21 import javax.swing.JTextField;
 22 import javax.swing.SwingConstants;
 23 import javax.swing.UIManager;
 24 import javax.swing.border.EtchedBorder;
 25 import javax.swing.border.LineBorder;
 26 import javax.swing.border.TitledBorder;
 27 import com.lzw.BackgroundPanel;
 28 import com.swtdesigner.SwingResourceManager;
 29 
 30 public class MainFrame extends JFrame {
 31 
 32     private JTextField textField_3;
 33     private JTextField textField_1;
 34     private JComboBox comboBox_1;
 35     private JTextField textField;
 36     private JComboBox cityComboBox;
 37     private JComboBox comboBox;
 38 
 39     /**
 40      * Launch the application
 41      * 
 42      * @param args
 43      */
 44     public static void main(String args[]) {
 45         EventQueue.invokeLater(new Runnable() {
 46             public void run() {
 47                 try {
 48                     UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
 49                     MainFrame frame = new MainFrame();
 50                     frame.setVisible(true);
 51                 } catch (Exception e) {
 52                     e.printStackTrace();
 53                 }
 54             }
 55         });
 56     }
 57 
 58     /**
 59      * Create the frame
 60      */
 61     public MainFrame() {
 62         getContentPane().setLayout(null);
 63         setBounds(100, 100, 518, 379);
 64         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 65         //获取默认的市/县
 66         String province=(String)getProvince()[0];
 67         setTitle("输入指定省/直辖市查询对应的市县");
 68 
 69         final BackgroundPanel backgroundPanel = new BackgroundPanel();
 70         backgroundPanel.setImage(SwingResourceManager.getImage(MainFrame.class, "/images/background.jpg"));
 71         backgroundPanel.setBounds(0, 0, 510, 380);
 72         getContentPane().add(backgroundPanel);
 73 
 74         final JPanel panel = new JPanel();
 75         panel.setOpaque(false);
 76         panel.setBounds(36, 126, 438, 70);
 77         backgroundPanel.add(panel);
 78         panel.setLayout(null);
 79         panel.setBorder(new TitledBorder(null, "居住地", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
 80         cityComboBox = new JComboBox();
 81         cityComboBox.setBounds(245, 25, 124, 27);
 82         panel.add(cityComboBox);
 83         
 84         cityComboBox.setModel(new DefaultComboBoxModel(getCity(province)));
 85 
 86         comboBox = new JComboBox();
 87         comboBox.setBounds(25, 25, 124, 27);
 88         panel.add(comboBox);
 89         comboBox.addItemListener(new ItemListener() {
 90             public void itemStateChanged(final ItemEvent e) { // 选项状态更改事件
 91                 itemChange();
 92             }
 93         });
 94         comboBox.setModel(new DefaultComboBoxModel(getProvince())); // 添加省份信息
 95 
 96         final JLabel label = new JLabel();
 97         label.setText("省/直辖市");
 98         label.setBounds(155, 30, 66, 18);
 99         panel.add(label);
100 
101         final JLabel label_1 = new JLabel();
102         label_1.setText("市/县");
103         label_1.setBounds(375, 30, 37, 18);
104         panel.add(label_1);
105 
106         final JLabel label_2 = new JLabel();
107         label_2.setBounds(36, 43, 65, 18);
108         backgroundPanel.add(label_2);
109         label_2.setHorizontalAlignment(SwingConstants.RIGHT);
110         label_2.setHorizontalTextPosition(SwingConstants.LEADING);
111         label_2.setText("姓    名:");
112 
113         textField = new JTextField();
114         textField.setBounds(113, 38, 154, 28);
115         backgroundPanel.add(textField);
116 
117         final JLabel label_3 = new JLabel();
118         label_3.setBounds(36, 84, 65, 18);
119         backgroundPanel.add(label_3);
120         label_3.setHorizontalAlignment(SwingConstants.RIGHT);
121         label_3.setHorizontalTextPosition(SwingConstants.LEADING);
122         label_3.setText("性    别:");
123 
124         comboBox_1 = new JComboBox();
125         comboBox_1.setBounds(113, 81, 66, 25);
126         backgroundPanel.add(comboBox_1);
127         comboBox_1.setModel(new DefaultComboBoxModel(new String[] {"男", "女"}));
128 
129         final JLabel label_4 = new JLabel();
130         label_4.setBounds(36, 212, 65, 18);
131         backgroundPanel.add(label_4);
132         label_4.setHorizontalAlignment(SwingConstants.RIGHT);
133         label_4.setHorizontalTextPosition(SwingConstants.LEADING);
134         label_4.setText("详细地址:");
135 
136         textField_1 = new JTextField();
137         textField_1.setBounds(113, 208, 367, 28);
138         backgroundPanel.add(textField_1);
139 
140         final JLabel label_4_1 = new JLabel();
141         label_4_1.setBounds(36, 252, 65, 18);
142         backgroundPanel.add(label_4_1);
143         label_4_1.setHorizontalTextPosition(SwingConstants.LEADING);
144         label_4_1.setHorizontalAlignment(SwingConstants.RIGHT);
145         label_4_1.setText("E-mail:");
146 
147         textField_3 = new JTextField();
148         textField_3.setBounds(113, 248, 367, 27);
149         backgroundPanel.add(textField_3);
150 
151         final JButton button = new JButton();
152         button.setBounds(159, 289, 75, 28);
153         backgroundPanel.add(button);
154         button.setText("保存");
155 
156         final JButton button_1 = new JButton();
157         button_1.setBounds(265, 289, 75, 28);
158         backgroundPanel.add(button_1);
159         button_1.setText("重置");
160         //
161     }
162 
163     /**
164      * 获取省、直辖市,自治区
165      * 
166      * @return
167      */
168     public Object[] getProvince() {
169         Map<String, String[]> map = CityMap.model;// 获取省份信息保存到Map中
170         Set<String> set = map.keySet(); // 获取Map集合中的键,并以Set集合返回
171         Object[] province = set.toArray(); // 转换为数组
172         return province; // 返回获取的省份信息
173     }
174 
175     /**
176      * 获取指定省对应的市/县
177      * 
178      * @param selectProvince
179      * @return
180      */
181     public String[] getCity(String selectProvince) {
182         Map<String, String[]> map = CityMap.model; // 获取省份信息保存到Map中
183         String[] arrCity = map.get(selectProvince); // 获取指定键的值
184         return arrCity; // 返回获取的市/县
185     }
186 
187     private void itemChange() {
188         String selectProvince = (String) comboBox.getSelectedItem();
189         cityComboBox.removeAllItems(); // 清空市/县列表
190         String[] arrCity = getCity(selectProvince); // 获取市/县
191         cityComboBox.setModel(new DefaultComboBoxModel(arrCity)); // 重新添加市/县列表的值
192     }
193 
194 }

 

Java-Map映射集合实现省市级联选择框

标签:you   []   tle   out   外观   上海   exception   round   jlist   

原文地址:https://www.cnblogs.com/susmote/p/8298172.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!