实例8Map映射集合实现省市级联选择框

/ java精彩编程200例 / 没有评论 / 147浏览
实例008Map映射集合实现省市级联选择框



写了好几个JFrame这种窗体类型的程序,我只找到了一丝丝灵性,java真的是越探索越广阔,每个程序都有新的点,让人热血沸腾。

这一次的代码依然是看的别人的,然后自己比着打。现在发现很多都是固定的每次都要用的,所以我就没写注释。因此这次的代码虽然很长,真正的关键就是三个方法和

这次用的Map集合真的有点秀,再就是组合框combobox的使用与集合的联用,还有就是集合与数组之间的转换,其他大多都是老东西了。

package java08;

import java.awt.BorderLayout; import java.awt.EventQueue; import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.border.TitledBorder; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.SwingConstants; import javax.swing.JComboBox; import javax.swing.ComboBoxEditor; import javax.swing.DefaultComboBoxModel; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.event.ActionEvent; import javax.swing.JOptionPane;

//本例中因为省与市是一对多的关系,直辖市是一对一 //在处理这种对应关系时用Map数据结构的键值对非常适合

//初始化Map集合对象
class CityMap{

//初始化有序的哈希Map   key值是字符串   value值是含各个市的字符串数组
public static Map<String, String[]> model = new LinkedHashMap<String, String[]>();

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

}

public class MainFrame extends JFrame { //在类中创建部分所需控件(方便方法中调用) private JPanel contentPane; private JTextField textField; private JTextField textField_1; private JTextField textField_2; private JComboBox<String> provincecomboBox; private JComboBox<String> citycomboBox;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                MainFrame frame = new MainFrame();//创建对象调用构造函数
                frame.setVisible(true);
                frame.setTitle("输入指定省/直辖市查询对应的市/县");//程序顶部标题
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

//得到所有省份的信息
public Object[] getProvince() {
    //model的key值 ----》 set ----》Object数组 
    Map&lt;String, String[]&gt; map = CityMap.model;//直接将CityMap类的model集合全部给新的局部Map变量map
    Set&lt;String&gt; set = map.keySet();//将map的key值给Set集合的变量set
    Object[] province = set.toArray();//再将set转化为Object[]数组province
    return province;//最后返回的是一个Object数组
}

//得到某省下的所有市信息
public String[] getCity(String selectProvince) {
    Map&lt;String, String[]&gt; map = CityMap.model;
    String[] arrCity = map.get(selectProvince);//get(key)得到对应省   下   一堆市的值
    return arrCity;        
}

//省 对应  下拉列表框的事件处理方法
public void itemChange() {
    String selectProvince = (String)provincecomboBox.getSelectedItem();//将选中的下拉列表的项目值以字符串传到selectprovince
    citycomboBox.removeAllItems(); // 清空市/县列表,为改变省的对应市的显示做准备   
    String[] arrCity = getCity(selectProvince); //把新选中的省对应的市取出放到arrCity字符串数组中
    citycomboBox.setModel(new DefaultComboBoxModel(arrCity)); // 重新添加市/县列表的值
}

/**
 * Create the frame.
 */
public MainFrame() {//构造函数
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//普通关闭
    String province=(String)getProvince()[0];//得到初始默认第一个省
    setBounds(100, 100, 450, 362);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(null);
    setContentPane(contentPane);

    JLabel label = new JLabel("\u59D3\u540D");
    label.setBounds(10, 46, 54, 15);
    contentPane.add(label);

    JLabel label_1 = new JLabel("\u6027\u522B");
    label_1.setBounds(10, 93, 54, 15);
    contentPane.add(label_1);

    JLabel label_4 = new JLabel("\u8BE6\u7EC6\u5730\u5740");
    label_4.setBounds(33, 229, 54, 15);
    contentPane.add(label_4);

    JLabel lblEmail = new JLabel("E-mail");
    lblEmail.setBounds(33, 269, 54, 15);
    contentPane.add(lblEmail);

    textField = new JTextField();
    textField.setBounds(60, 43, 115, 21);
    contentPane.add(textField);
    textField.setColumns(10);

    textField_1 = new JTextField();
    textField_1.setBounds(111, 226, 213, 21);
    contentPane.add(textField_1);
    textField_1.setColumns(10);

    textField_2 = new JTextField();
    textField_2.setBounds(111, 266, 213, 21);
    contentPane.add(textField_2);
    textField_2.setColumns(10);

    JComboBox sexcomboBox = new JComboBox();
    sexcomboBox.setBounds(60, 90, 47, 21);
    contentPane.add(sexcomboBox);
    sexcomboBox.setModel(new DefaultComboBoxModel(new String[] {"男","女"}));//初始化下拉列表为男和女

    JButton btnNewButton = new JButton("\u4FDD\u5B58");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            //弹出提示信息   对应参数:所属类    显示信息    顶部标题    提示窗类型
            JOptionPane.showMessageDialog(null, "保存成功!","提示信息",JOptionPane.INFORMATION_MESSAGE);
        }
    });
    btnNewButton.setBounds(111, 300, 93, 23);
    contentPane.add(btnNewButton);

    JButton btnNewButton_1 = new JButton("\u9000\u51FA");
    btnNewButton_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {//退出
            System.exit(0);
        }
    });
    btnNewButton_1.setBounds(243, 300, 93, 23);
    contentPane.add(btnNewButton_1);

    JPanel panel = new JPanel();
    panel.setOpaque(false);
    panel.setBounds(21, 146, 403, 70);        
    contentPane.add(panel);
    panel.setLayout(null);
    //设置面板的标题
    panel.setBorder(new TitledBorder(null,"居住地",TitledBorder.DEFAULT_JUSTIFICATION,TitledBorder.DEFAULT_POSITION,null,null));

    JLabel label_2 = new JLabel("\u7701/\u76F4\u8F96\u5E02");
    label_2.setBounds(105, 10, 99, 50);
    panel.add(label_2);

    citycomboBox = new JComboBox();
    citycomboBox.setBounds(212,25, 85,21);
    panel.add(citycomboBox);

    //将初始默认第一个省的所有市显示
    citycomboBox.setModel(new DefaultComboBoxModel&lt;String&gt;(getCity(province)));        

    JLabel label_3 = new JLabel("\u5E02/\u53BF");
    label_3.setBounds(307, 10, 50, 50);
    panel.add(label_3);

    provincecomboBox = new JComboBox();
    provincecomboBox.setBounds(10, 25, 85, 21);
    panel.add(provincecomboBox);

    //将整个province放入下拉框,正好对应第一个省就默认显示了
    provincecomboBox.setModel(new DefaultComboBoxModel(getProvince()));

    provincecomboBox.addItemListener(new ItemListener() {//下拉所选内容对应    事件        
        @Override
        public void itemStateChanged(ItemEvent e) {
            // TODO Auto-generated method stub
            itemChange();
        }
    });
}

}

实现结果:

切换省市并点击按钮保存:


那个男孩,教会我成长:那个女孩,教会我爱......