java jframe 城市三级联动

public class Sanji extends JFrame {

	private JPanel contentPane;
	private JComboBox shengBox;
	private JComboBox shiboBox;
	private String[] shengarr;
	//存放省份ProID的map集合
	private Map<String, Integer> proidmap=new HashMap<String, Integer>(); 
    //存放市cityID的集合
	private Map<String, Integer> citymap=new HashMap<String,Integer>();
	private String[] shiarr;//市数组
	private JComboBox comboBox;
	private JLabel label_2;
	private String[] quarr;//区数组
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Sanji frame = new Sanji();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Sanji() {
		getsheng();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		shengBox = new JComboBox();
		//省份的监听事件
		shengBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			   String cityname=(String) shengBox.getSelectedItem();//获得省份框对象
			   int id=proidmap.get(cityname);
			   getshi(id);//调用获得市的方法
			   shiboBox.setModel(new DefaultComboBoxModel(shiarr));//向市下拉框添加数据
			}
		});
		shengBox.setModel(new DefaultComboBoxModel(shengarr));
		shengBox.setBounds(10, 31, 76, 21);
		contentPane.add(shengBox);
		
		JLabel label = new JLabel("\u7701");
		label.setBounds(96, 34, 54, 15);
		contentPane.add(label);
		//市的监听事件
		shiboBox = new JComboBox();
		shiboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String str=(String) shiboBox.getSelectedItem();
				int id=citymap.get(str);
				getqu(id);
				comboBox.setModel(new DefaultComboBoxModel(quarr));
			}
		});
		
		shiboBox.setBounds(135, 31, 76, 21);
		contentPane.add(shiboBox);
		
		JLabel label_1 = new JLabel("\u5E02");
		label_1.setBounds(221, 34, 54, 15);
		contentPane.add(label_1);
		
		comboBox = new JComboBox();
		
		comboBox.setBounds(280, 31, 63, 21);
		contentPane.add(comboBox);
		
		label_2 = new JLabel("\u533A");
		label_2.setBounds(353, 34, 54, 15);
		contentPane.add(label_2);
	}
	//获得省
	public void getsheng(){
		try {
			JsonParser jp =new JsonParser();
			FileReader fr =new FileReader("city.json");//json文件
			JsonObject jo=(JsonObject) jp.parse(fr);
			JsonArray sheng =jo.get("省").getAsJsonArray();
			shengarr=new String[sheng.size()];
			for (int i = 0; i < sheng.size(); i++) {
				JsonObject jo2=(JsonObject) sheng.get(i);
				String name=jo2.get("name").getAsString();
				int proid=jo2.get("ProID").getAsInt();
				shengarr[i]=name;
				proidmap.put(name, proid);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//获得市
	public void getshi(int proid){
		JsonParser jp =new JsonParser();
		try {
			FileReader fr =new FileReader("city.json");
			JsonObject jo=(JsonObject) jp.parse(fr);
			JsonArray shi =jo.get("市").getAsJsonArray();
			shengarr=new String[shi.size()];
			StringBuffer sb=new StringBuffer();
			for (int i = 0; i < shi.size(); i++) {
				JsonObject jo2=(JsonObject) shi.get(i);
				int id=jo2.get("ProID").getAsInt();
				int cityid=jo2.get("CityID").getAsInt();
				String cityname=jo2.get("name").getAsString();
				citymap.put(cityname, cityid);
				if(id==proid){
				String shiname=jo2.get("name").getAsString();
					sb.append(shiname+",");
			}
			}
			shiarr=sb.toString().split(",");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		}
	//获得区的方法
	public void getqu(int id){
		try {
			JsonParser jp =new JsonParser();
			FileReader fr =new FileReader("city.json");
			JsonObject jo=(JsonObject) jp.parse(fr);
			JsonArray qu =jo.get("区").getAsJsonArray();
			shengarr=new String[qu.size()];
			StringBuffer sb=new StringBuffer();
			for (int i = 0; i < qu.size(); i++) {
				JsonObject jo2=(JsonObject) qu.get(i);
				int cityid=jo2.get("CityID").getAsInt();
				if(cityid==id){
					String name=jo2.get("DisName").getAsString();
					sb.append(name+",");
			}
			}
			quarr=sb.toString().split(",");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}



Java复选框三级联动可以通过监听器和递归实现。以下是一个简单的示例代码,供参考: ```java import javax.swing.*; import javax.swing.event.*; import java.awt.*; public class CheckboxTest extends JFrame { private JCheckBox[] provinceCheckbox = { new JCheckBox("江苏"), new JCheckBox("浙江"), new JCheckBox("上海") }; private JCheckBox[][] cityCheckbox = { { new JCheckBox("南京"), new JCheckBox("苏州"), new JCheckBox("无锡") }, { new JCheckBox("杭州"), new JCheckBox("宁波"), new JCheckBox("温州") }, { new JCheckBox("上海市") } }; private JCheckBox[][][] districtCheckbox = { { { new JCheckBox("玄武区"), new JCheckBox("秦淮区"), new JCheckBox("建邺区") }, { new JCheckBox("常熟市"), new JCheckBox("张家港市"), new JCheckBox("昆山市") }, { new JCheckBox("惠山区"), new JCheckBox("锡山区"), new JCheckBox("滨湖区") } }, { { new JCheckBox("西湖区"), new JCheckBox("江干区"), new JCheckBox("下城区") }, { new JCheckBox("江北区"), new JCheckBox("海曙区"), new JCheckBox("鄞州区") }, { new JCheckBox("鹿城区"), new JCheckBox("龙湾区"), new JCheckBox("瓯海区") } }, { { new JCheckBox("黄浦区"), new JCheckBox("徐汇区"), new JCheckBox("长宁区") } } }; public CheckboxTest() { JPanel panel = new JPanel(new GridLayout(0, 2)); for (JCheckBox checkBox : provinceCheckbox) { panel.add(checkBox); checkBox.addItemListener(new CheckboxListener(cityCheckbox[getIndex(provinceCheckbox, checkBox)])); } add(panel); setSize(400, 150); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private int getIndex(Object[] array, Object object) { for (int i = 0; i < array.length; i++) { if (array[i] == object) { return i; } } return -1; } private class CheckboxListener implements ItemListener { private JCheckBox[] cityCheckbox; public CheckboxListener(JCheckBox[] cityCheckbox) { this.cityCheckbox = cityCheckbox; } @Override public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { JPanel panel = (JPanel) getContentPane().getComponent(0); for (JCheckBox checkBox : cityCheckbox) { panel.add(checkBox); checkBox.addItemListener(new CheckboxListener2(districtCheckbox[getIndex(provinceCheckbox, e.getItem())][getIndex(cityCheckbox, checkBox)])); } revalidate(); repaint(); } else { JPanel panel = (JPanel) getContentPane().getComponent(0); for (JCheckBox checkBox : cityCheckbox) { panel.remove(checkBox); } revalidate(); repaint(); } } } private class CheckboxListener2 implements ItemListener { private JCheckBox[] districtCheckbox; public CheckboxListener2(JCheckBox[] districtCheckbox) { this.districtCheckbox = districtCheckbox; } @Override public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { JPanel panel = (JPanel) getContentPane().getComponent(0); for (JCheckBox checkBox : districtCheckbox) { panel.add(checkBox); } revalidate(); repaint(); } else { JPanel panel = (JPanel) getContentPane().getComponent(0); for (JCheckBox checkBox : districtCheckbox) { panel.remove(checkBox); } revalidate(); repaint(); } } } public static void main(String[] args) { new CheckboxTest(); } } ``` 在这个示例中,我们创建了三个省份复选框,每个省份下面有若干个城市复选框,每个城市下面有若干个区县复选框。当选择一个省份时,该省份下面的城市复选框会出现,当选择一个城市时,该城市下面的区县复选框会出现。整个过程是通过监听器实现的,当复选框状态改变时,监听器会相应地增加或删除与之关联的复选框。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值