NetBeans IDE 6.8 JAVA Swing控件三级联动下拉列表怎么写?
我现在还在学习初级阶段,很多地方还不懂,现在做到两个下拉列表的联动,但是不知道怎么写第三个。就是做一个学生信息注册表,系部,专业,班级三个下拉列表,现在做好了系部和专业两...
我现在还在学习初级阶段,很多地方还不懂,现在做到两个下拉列表的联动,但是不知道怎么写第三个。 就是做一个学生信息注册表,系部,专业,班级三个下拉列表,现在做好了系部和专业两个下拉列表,到班级的时候就卡住了。记得答的简单些,我还不太懂,现在还没连上数据库,我用的是if和else写的联动,代码都是写死了的。希望懂的亲能够解答一下怎么写三级联动。
展开
展开全部
用else if不行啊,数据库里如果“系部”有30几个,难道你一个个写过去吗?可以用for循环,把系部加入第一个列表然后 对该列表添加监听器 addActionListener 通过该列表.getSelectedItem得到选中的项 然后专业可以写一个类。里面有一个属性是“系部” 当那些个专业的系部属性为 第一个列表中的值的时候把那些专业的“专业名”属性加入第二个列表中 依次类推我给你一个关于省份,城市,和营业网点的3级下拉列表的例子,你稍微修改就可以了
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel; @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();
this.add(panel);
//panel.setLayout(new GridLayout(3,2));
panel.add(new JLabel("省份:"));
panel.add(provinceBox);
panel.add(new JLabel("城市:"));
panel.add(cityBox);
panel.add(new JLabel("营业网点"));
panel.add(branchBox);
}
private void initData(){//初始化省份 城市和营业网点 这些数据实际上应该从数据库里查询
provinces.add("浙江");
provinces.add("广西");
citys.add(new City("浙江","宁波"));
citys.add(new City("浙江","金华"));
citys.add(new City("广西","桂林"));
citys.add(new City("广西","南宁"));
branchs.add(new Branch("浙江","宁波", "宁波镇海网点"));
branchs.add(new Branch("浙江","宁波", "宁波北仑网点"));
branchs.add(new Branch("浙江","金华", "金华网点1"));
branchs.add(new Branch("浙江","金华", "金华网点2"));
branchs.add(new Branch("广西","桂林", "桂林网点1"));
branchs.add(new Branch("广西","桂林", "桂林网点2"));
branchs.add(new Branch("广西","南宁", "南宁网点1"));
branchs.add(new Branch("广西","南宁", "南宁网点2"));
}
private void setBox(){//设置下拉列表里的值
//设置先在各项列表里加入的值
provinceBox.removeAllItems();
cityBox.removeAllItems();
branchBox.removeAllItems();
provinceBox.addItem("所有");
cityBox.addItem("所有");
branchBox.addItem("所有");
for (int i = 0; i < provinces.size(); i++) {
provinceBox.addItem(provinces.get(i));
}
for (int i = 0; i < citys.size(); i++) {
cityBox.addItem(citys.get(i).getCity());
}
for (int i = 0; i < branchs.size(); i++) {
branchBox.addItem(branchs.get(i).getBranch());
}
}
private void addHandler(){//设置3个下拉列表的级联关系
provinceBox.addActionListener(new ActionListener() {
//监听province下拉列表
@Override
public void actionPerformed(ActionEvent e) {
cityBox.removeAllItems();
cityBox.addItem("所有");
branchBox.removeAllItems();
branchBox.addItem("所有");
String province=provinceBox.getSelectedItem().toString();//得到当前点中的值
//在city下拉列表中 遍历citys,如果citys[i]的省份属性和它相同则添加进去,其中要考虑是所有的特殊情况
if(province.equals("所有")){
for (int i = 0; i < citys.size(); i++) {
cityBox.addItem(citys.get(i).getCity());
}
}else{
for (int i = 0; i < citys.size(); i++) {
if(citys.get(i).getProvince().equals(province)){
cityBox.addItem(citys.get(i).getCity());
}
}
}
//在branch下拉列表中 遍历branchs,如果branchs[i]的省份属性和它相同则添加进去,其中要考虑是所有的特殊情况
if(province.equals("所有")){
for (int i = 0; i < branchs.size(); i++) {
branchBox.addItem(branchs.get(i).getBranch());
}
}else{
for (int i = 0; i < branchs.size(); i++) {
if(branchs.get(i).getProvince().equals(province)){
branchBox.addItem(branchs.get(i).getBranch());
}
}
}
}
});
//监听cityBox下拉列表
cityBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
branchBox.removeAllItems();
branchBox.addItem("所有");
if(cityBox.getSelectedItem()==null){
return ;
}
String city=cityBox.getSelectedItem().toString();//得到当前点中的值
if(city.equals("所有")){//如果是所有,则看省份框中是什么值
if(provinceBox.getSelectedItem().equals("所有")){
for (int i = 0; i < branchs.size(); i++) {
branchBox.addItem(branchs.get(i).getBranch());
}
}else{
for (int i = 0; i < branchs.size(); i++) {
if(branchs.get(i).getProvince().equals(provinceBox.getSelectedItem())){
branchBox.addItem(branchs.get(i).getBranch());
}
}
}
}else{
for (int i = 0; i < branchs.size(); i++) {
if(city.equals(branchs.get(i).getCity())){
branchBox.addItem(branchs.get(i).getBranch());
}
}
}
}
});
}
private void showMe(){
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
}
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();
this.province = province;
this.city = city;
} public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
}
class Branch{//网点类
private String province;//所属省份
private String city;//所属城市
private String branch;//网点名 public Branch(String province, String city, String branch) {
super();
this.province = province;
this.city = city;
this.branch = branch;
}
public String getBranch() {
return branch;
}
public void setBranch(String branch) {
this.branch = branch;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel; @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();
this.add(panel);
//panel.setLayout(new GridLayout(3,2));
panel.add(new JLabel("省份:"));
panel.add(provinceBox);
panel.add(new JLabel("城市:"));
panel.add(cityBox);
panel.add(new JLabel("营业网点"));
panel.add(branchBox);
}
private void initData(){//初始化省份 城市和营业网点 这些数据实际上应该从数据库里查询
provinces.add("浙江");
provinces.add("广西");
citys.add(new City("浙江","宁波"));
citys.add(new City("浙江","金华"));
citys.add(new City("广西","桂林"));
citys.add(new City("广西","南宁"));
branchs.add(new Branch("浙江","宁波", "宁波镇海网点"));
branchs.add(new Branch("浙江","宁波", "宁波北仑网点"));
branchs.add(new Branch("浙江","金华", "金华网点1"));
branchs.add(new Branch("浙江","金华", "金华网点2"));
branchs.add(new Branch("广西","桂林", "桂林网点1"));
branchs.add(new Branch("广西","桂林", "桂林网点2"));
branchs.add(new Branch("广西","南宁", "南宁网点1"));
branchs.add(new Branch("广西","南宁", "南宁网点2"));
}
private void setBox(){//设置下拉列表里的值
//设置先在各项列表里加入的值
provinceBox.removeAllItems();
cityBox.removeAllItems();
branchBox.removeAllItems();
provinceBox.addItem("所有");
cityBox.addItem("所有");
branchBox.addItem("所有");
for (int i = 0; i < provinces.size(); i++) {
provinceBox.addItem(provinces.get(i));
}
for (int i = 0; i < citys.size(); i++) {
cityBox.addItem(citys.get(i).getCity());
}
for (int i = 0; i < branchs.size(); i++) {
branchBox.addItem(branchs.get(i).getBranch());
}
}
private void addHandler(){//设置3个下拉列表的级联关系
provinceBox.addActionListener(new ActionListener() {
//监听province下拉列表
@Override
public void actionPerformed(ActionEvent e) {
cityBox.removeAllItems();
cityBox.addItem("所有");
branchBox.removeAllItems();
branchBox.addItem("所有");
String province=provinceBox.getSelectedItem().toString();//得到当前点中的值
//在city下拉列表中 遍历citys,如果citys[i]的省份属性和它相同则添加进去,其中要考虑是所有的特殊情况
if(province.equals("所有")){
for (int i = 0; i < citys.size(); i++) {
cityBox.addItem(citys.get(i).getCity());
}
}else{
for (int i = 0; i < citys.size(); i++) {
if(citys.get(i).getProvince().equals(province)){
cityBox.addItem(citys.get(i).getCity());
}
}
}
//在branch下拉列表中 遍历branchs,如果branchs[i]的省份属性和它相同则添加进去,其中要考虑是所有的特殊情况
if(province.equals("所有")){
for (int i = 0; i < branchs.size(); i++) {
branchBox.addItem(branchs.get(i).getBranch());
}
}else{
for (int i = 0; i < branchs.size(); i++) {
if(branchs.get(i).getProvince().equals(province)){
branchBox.addItem(branchs.get(i).getBranch());
}
}
}
}
});
//监听cityBox下拉列表
cityBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
branchBox.removeAllItems();
branchBox.addItem("所有");
if(cityBox.getSelectedItem()==null){
return ;
}
String city=cityBox.getSelectedItem().toString();//得到当前点中的值
if(city.equals("所有")){//如果是所有,则看省份框中是什么值
if(provinceBox.getSelectedItem().equals("所有")){
for (int i = 0; i < branchs.size(); i++) {
branchBox.addItem(branchs.get(i).getBranch());
}
}else{
for (int i = 0; i < branchs.size(); i++) {
if(branchs.get(i).getProvince().equals(provinceBox.getSelectedItem())){
branchBox.addItem(branchs.get(i).getBranch());
}
}
}
}else{
for (int i = 0; i < branchs.size(); i++) {
if(city.equals(branchs.get(i).getCity())){
branchBox.addItem(branchs.get(i).getBranch());
}
}
}
}
});
}
private void showMe(){
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
}
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();
this.province = province;
this.city = city;
} public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
}
class Branch{//网点类
private String province;//所属省份
private String city;//所属城市
private String branch;//网点名 public Branch(String province, String city, String branch) {
super();
this.province = province;
this.city = city;
this.branch = branch;
}
public String getBranch() {
return branch;
}
public void setBranch(String branch) {
this.branch = branch;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询