import javaawteventActionEvent;
import javaawteventActionListener;
import javautilArrayList;
import javautilList;import javaxswingJComboBox;
import javaxswingJFrame;
import javaxswingJLabel;
import javaxswingJPanel; @SuppressWarnings("serial")
public class Test extends JFrame{
private List<String> provinces=new ArrayList<String>();
private List<City> citys=new ArrayList<City>();
private List<Branch> branchs=new ArrayList<Branch>();
private JComboBox provinceBox=new JComboBox();
private JComboBox cityBox=new JComboBox();
private JComboBox branchBox=new JComboBox();
public Test() {
init();
initData();
setBox();
addHandler();
}
public void init(){//初始化GUI界面
JPanel panel=new JPanel();
thisadd(panel);
//panelsetLayout(new GridLayout(3,2));
paneladd(new JLabel("省份:"));
paneladd(provinceBox);
paneladd(new JLabel("城市:"));
paneladd(cityBox);
paneladd(new JLabel("营业网点"));
paneladd(branchBox);
}
private void initData(){//初始化省份 城市和营业网点 这些数据实际上应该从数据库里查询
provincesadd("浙江");
provincesadd("广西");
citysadd(new City("浙江","宁波"));
citysadd(new City("浙江","金华"));
citysadd(new City("广西","桂林"));
citysadd(new City("广西","南宁"));
branchsadd(new Branch("浙江","宁波", "宁波镇海网点"));
branchsadd(new Branch("浙江","宁波", "宁波北仑网点"));
branchsadd(new Branch("浙江","金华", "金华网点1"));
branchsadd(new Branch("浙江","金华", "金华网点2"));
branchsadd(new Branch("广西","桂林", "桂林网点1"));
branchsadd(new Branch("广西","桂林", "桂林网点2"));
branchsadd(new Branch("广西","南宁", "南宁网点1"));
branchsadd(new Branch("广西","南宁", "南宁网点2"));
}
private void setBox(){//设置下拉列表里的值
//设置先在各项列表里加入的值
provinceBoxremoveAllItems();
cityBoxremoveAllItems();
branchBoxremoveAllItems();
provinceBoxaddItem("所有");
cityBoxaddItem("所有");
branchBoxaddItem("所有");
for (int i = 0; i < provincessize(); i++) {
provinceBoxaddItem(provincesget(i));
}
for (int i = 0; i < cityssize(); i++) {
cityBoxaddItem(citysget(i)getCity());
}
for (int i = 0; i < branchssize(); i++) {
branchBoxaddItem(branchsget(i)getBranch());
}
}
private void addHandler(){//设置3个下拉列表的级联关系
provinceBoxaddActionListener(new ActionListener() {
//监听province下拉列表
@Override
public void actionPerformed(ActionEvent e) {
cityBoxremoveAllItems();
cityBoxaddItem("所有");
branchBoxremoveAllItems();
branchBoxaddItem("所有");
String province=provinceBoxgetSelectedItem()toString();//得到当前点中的值
//在city下拉列表中 遍历citys,如果citys[i]的省份属性和它相同则添加进去,其中要考虑是所有的特殊情况
if(provinceequals("所有")){
for (int i = 0; i < cityssize(); i++) {
cityBoxaddItem(citysget(i)getCity());
}
}else{
for (int i = 0; i < cityssize(); i++) {
if(citysget(i)getProvince()equals(province)){
cityBoxaddItem(citysget(i)getCity());
}
}
}
//在branch下拉列表中 遍历branchs,如果branchs[i]的省份属性和它相同则添加进去,其中要考虑是所有的特殊情况
if(provinceequals("所有")){
for (int i = 0; i < branchssize(); i++) {
branchBoxaddItem(branchsget(i)getBranch());
}
}else{
for (int i = 0; i < branchssize(); i++) {
if(branchsget(i)getProvince()equals(province)){
branchBoxaddItem(branchsget(i)getBranch());
}
}
}
}
});
//监听cityBox下拉列表
cityBoxaddActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
branchBoxremoveAllItems();
branchBoxaddItem("所有");
if(cityBoxgetSelectedItem()==null){
return ;
}
String city=cityBoxgetSelectedItem()toString();//得到当前点中的值
if(cityequals("所有")){//如果是所有,则看省份框中是什么值
if(provinceBoxgetSelectedItem()equals("所有")){
for (int i = 0; i < branchssize(); i++) {
branchBoxaddItem(branchsget(i)getBranch());
}
}else{
for (int i = 0; i < branchssize(); i++) {
if(branchsget(i)getProvince()equals(provinceBoxgetSelectedItem())){
branchBoxaddItem(branchsget(i)getBranch());
}
}
}
}else{
for (int i = 0; i < branchssize(); i++) {
if(cityequals(branchsget(i)getCity())){
branchBoxaddItem(branchsget(i)getBranch());
}
}
}
}
});
}
private void showMe(){
thissetVisible(true);
thissetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
thispack();
}
public static void main(String[] args) {
new Test()showMe();
}
} //下面是要用的2个类
class City{//城市类
private String city;//城市名
private String province;//所属省份
public City() {
// TODO Auto-generated constructor stub
}
public City(String province,String city) {
super();
thisprovince = province;
thiscity = city;
} public String getCity() {
return city;
}
public void setCity(String city) {
thiscity = city;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
thisprovince = province;
}
}
class Branch{//网点类
private String province;//所属省份
private String city;//所属城市
private String branch;//网点名 public Branch(String province, String city, String branch) {
super();
thisprovince = province;
thiscity = city;
thisbranch = branch;
}
public String getBranch() {
return branch;
}
public void setBranch(String branch) {
thisbranch = branch;
}
public String getCity() {
return city;
}
public void setCity(String city) {
thiscity = city;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
thisprovince = province;
}
}给你个例子,里面的数据自己改下
==============================
<html>
<head>
</head>
<body>
<script language="JavaScript">
<!--
//第一维:第一个下拉列表的值
//第二维:表示下拉列表中看到的字符串
//第三维:表示下拉列表中的值
var subcat = new Array();
//如果大类的值是动态的,则可以通过数据库中取出作如下设置
//subcat[<%=count%>] = new Array('<%=trim(rs("leemaid"))%>','<%=rs("leemaconte nt")%>','<%=rs("leemacontent")%>');
subcat[0] = new Array('1','电视机','=1')
subcat[1] = new Array('1','电风扇','=2')
subcat[2] = new Array('1','电饭煲','=3')
subcat[3] = new Array('1','电炒锅','=4')
subcat[4] = new Array('1','电灯','=5')
subcat[5] = new Array('1','电脑','=6')
subcat[6] = new Array('1','电冰箱','=7')
subcat[7] = new Array('1','洗衣机','=8')
subcat[8] = new Array('1','微波炉','=9')
subcat[9] = new Array('1','VCD影碟机','=1')
subcat[10] = new Array('2','二极管','=11')
subcat[11] = new Array('2','三极管','=12')
subcat[12] = new Array('2','CMOS集成块','=13')
subcat[13] = new Array('2','电阻','=14')
subcat[14] = new Array('2','电容','=15')
subcat[15] = new Array('2','整流二极管','=16')
subcat[16] = new Array('2','整流桥','=17')
subcat[17] = new Array('2','可控硅','=18')
subcat[18] = new Array('2','电子q','=19')
subcat[19] = new Array('2','光敏二极管','=2')
subcat[20] = new Array('3','ffffff','vvvvvv')
subcat[21] = new Array('3','dddddd','ffffff')
function changeselect1(selectValue)
{
documentform1s2length = 0;//初始化下拉列表 清空下拉数据
//documentform1s2options[0] = new Option('请选择二级小类','');//给第一个值
for (i=0; i<subcatlength; i++)//legth=2
{
if (subcat[i][0] == selectValue)//[0] [1] 第一列 第二列
{
documentform1s2options[documentform1s2length] = new Option(subcat[i][1], subcat[i][2]);
}
}
}
//-->
</script>
<form name="form1">
二级联动:
<select name="s1" onChange="changeselect1(thisvalue)">
<option>请选择一级大类</option>
<option value="1">家用电器</option>
<option value="2">电子元器件</option>
<option value="3">ddddd</option>
</select>
<select name="s2" onChange="alert(thisvalue)">
<option>请选择二级小类</option>
</select>
</form>
</html>
效果图
参考代码
import javaawtevent;
import javautilVector;
import javaxswing;
public class Demo extends JFrame implements ActionListener {
JButton btn;
Vector<String> vector=new Vector<String>();
JComboBox<String> jcb;
public Demo() {
vectoradd("公斤");
vectoradd("每个");
vectoradd("每盒");
JLabel jl = new JLabel("单位");
jcb = new JComboBox<String>(vector);//可以传入一个数组或者一个Vector链表,对下拉框进行初始化
btn=new JButton("获取下拉框的文字");
btnaddActionListener(this);
JPanel jp = new JPanel();
jpadd(jl);
jpadd(jcb);
jpadd(btn);
add(jp);
setSize(300, 200);
setLocationRelativeTo(null);
setTitle("下拉框Demo");
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Demo()setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
JOptionPaneshowMessageDialog(this, (String)jcbgetSelectedItem());
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)