Java基础知识:Java实现Map集合二级联动4

 comboBox.setModel(new DefaultComboBoxModel(getProvince())); // 添加省份信息

  final JLabel label = new JLabel();

  label.setText("省/直辖市");

  label.setBounds(155, 30, 66, 18);

  panel.add(label);

  final JLabel label_1 = new JLabel();

  label_1.setText("市/县");

  label_1.setBounds(375, 30, 37, 18);

  panel.add(label_1);

  final JLabel label_2 = new JLabel();

  label_2.setBounds(36, 43, 65, 18);

  backgroundPanel.add(label_2);

  label_2.setHorizontalAlignment(SwingConstants.RIGHT);

  label_2.setHorizontalTextPosition(SwingConstants.LEADING);

  label_2.setText("姓 名:");

  textField = new JTextField();

  textField.setBounds(113, 38, 154, 28);

  backgroundPanel.add(textField);

  final JLabel label_3 = new JLabel();

  label_3.setBounds(36, 84, 65, 18);

  backgroundPanel.add(label_3);

  label_3.setHorizontalAlignment(SwingConstants.RIGHT);

  label_3.setHorizontalTextPosition(SwingConstants.LEADING);

  label_3.setText("性 别:");

  comboBox_1 = new JComboBox();

  comboBox_1.setBounds(113, 81, 66, 25);

  backgroundPanel.add(comboBox_1);

  comboBox_1.setModel(new DefaultComboBoxModel(new String[] {"男", "女"}));

  final JLabel label_4 = new JLabel();

  label_4.setBounds(36, 212, 65, 18);

  backgroundPanel.add(label_4);

  label_4.setHorizontalAlignment(SwingConstants.RIGHT);

  label_4.setHorizontalTextPosition(SwingConstants.LEADING);

  label_4.setText("详细地址:");

  textField_1 = new JTextField();

  textField_1.setBounds(113, 208, 367, 28);

  backgroundPanel.add(textField_1);

  final JLabel label_4_1 = new JLabel();

  label_4_1.setBounds(36, 252, 65, 18);

  backgroundPanel.add(label_4_1);

  label_4_1.setHorizontalTextPosition(SwingConstants.LEADING);

  label_4_1.setHorizontalAlignment(SwingConstants.RIGHT);

  label_4_1.setText("E-mail:");

  textField_3 = new JTextField();

  textField_3.setBounds(113, 248, 367, 27);

  backgroundPanel.add(textField_3);

  final JButton button = new JButton();

  button.setBounds(159, 289, 75, 28);

  backgroundPanel.add(button);

  button.setText("保存");

  final JButton button_1 = new JButton();

  button_1.setBounds(265, 289, 75, 28);

  backgroundPanel.add(button_1);

  button_1.setText("重置");

  //

  }

  /**

  * 获取省、直辖市,自治区

  *

  * @return

  */

  public Object[] getProvince() {

  Map map = CityMap.model;// 获取省份信息保存到Map中

  Set set = map.keySet(); // 获取Map集合中的键,并以Set集合返回

  Object[] province = set.toArray(); // 转换为数组

  return province; // 返回获取的省份信息

  }

  /**

  * 获取指定省对应的市/县

  *

  * @param selectProvince

  * @return

  */

  public String[] getCity(String selectProvince) {

  Map map = CityMap.model; // 获取省份信息保存到Map中

  String[] arrCity = map.get(selectProvince); // 获取指定键的值

  return arrCity; // 返回获取的市/县

  }

  private void itemChange() {

  String selectProvince = (String) comboBox.getSelectedItem();

  cityComboBox.removeAllItems(); // 清空市/县列表

  String[] arrCity = getCity(selectProvince); // 获取市/县

  cityComboBox.setModel(new DefaultComboBoxModel(arrCity)); // 重新添加市/县列表的值

  }

  }

  效果如图:

原文地址:https://www.cnblogs.com/-zpy/p/5016277.html