Java实现省市联动部分功能参考代码

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
this.setTitle("省份联动");
this.setLocationRelativeTo(null);
this.setLayout(null);
this.init();
this.add(plbl);this.add(clbl);
this.add(pBox);this.add(cBox);
this.setVisible(true);
}
//初始化界面方法
public void init() {
this.plbl=new JLabel("省份:");
this.clbl=new JLabel("城市:");
this.pBox=new JComboBox();this.cBox=new JComboBox();
this.pcbm=new DefaultComboBoxModel();Leabharlann Baiduhis.ccbm=new DefaultComboBoxModel();
private List<City> cList;
private Manager manager;
//构造方法
public ShowView(){
manager=new Manager();
this.setSize(550, 400);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
//类的成员
private JLabel plbl;
private JLabel clbl;
private JComboBox pBox;
private JComboBox cBox;
private DefaultComboBoxModel pcbm,ccbm;
private List<Promary> pList;
}
}
}
}
import com.bean.City;
import com.bean.Promary;
import com.citymanager.Manager;
/**
*显示窗口
* @author CWB
*
*/
public class ShowView extends JFrame implements ItemListener{
//每次选择省份之前先对城市下拉框清空
this.cBox.removeAllItems();
for(Promary p:pList ){
if(this.pBox.getSelectedItem().equals(p.getPname())){
Promary promary=new Promary();
}
//注意!!!!!!!只需对前面的下拉框控件进行监听即可
this.pBox.addItemListener(this);//this.cBox.addItemListener(this);
}
@Override
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange()==1){
this.pBox.setModel(pcbm);
this.cBox.setModel(ccbm);
pcbm.addElement("请选择省份");
ccbm.addElement("所选省份的各个城市");
this.plbl.setBounds(60, 80, 45, 30);
this.clbl.setBounds(270, 80, 48, 30);
this.pBox.setBounds(100, 80, 155, 30);
this.cBox.setBounds(310, 80, 155, 30);
pList=manager.updatePromary();
for(Promary p:pList){
pcbm.addElement(p.getPname());
Java实现省市联动功能代码块
package com.view;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
promary.setPname(p.getPname());
//System.out.println(promary.getPname());
cList=manager.updateCity(p);
for(City c:cList){
ccbm.addElement(c.getCname());
}
}
相关文档
最新文档