java 二级联动_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)); // 重新添加市/县列表的值

}

}

效果如图:

2356158991.png

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值