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();
}
}
}