1:什么是组合框
如下图,类似这样的 就是组合框
2:实现上述这个组合框
2.1:代码实现
/**
* 组合框的学习
* */
package Swing;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class Main8 extends JFrame{
public static void main(String[] args) {
Main8 main8 = new Main8();
main8.run();
}
public void run(){
//创建其他组件
JButton button = new JButton("确定");
button.setBounds(170, 50, 75, 35);
//创建组合框数据容器
DefaultComboBoxModel<Object> ComboBoxModel = new DefaultComboBoxModel<>();
ComboBoxModel.addElement("张三");
ComboBoxModel.addElement("李四");
ComboBoxModel.addElement(123);
//创建组合框
JComboBox<Object> comboBox = new JComboBox<>(ComboBoxModel);
//设置位置
comboBox.setBounds(50, 50, 100, 35);
comboBox.setEditable(true);//设置组合框可编辑 默认为false
//添加监听器 打印选择
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//下面两个获取item的内容是一样的
System.out.println(comboBox.getSelectedItem());
System.out.println(ComboBoxModel.getSelectedItem());
}
});
//创建container容器
Container container = this.getContentPane();
container.add(comboBox);
container.add(button);
/**
* 窗体的设置
* */
this.setLayout(null);// 设置空布局管理器
this.setTitle("组合框学习");
this.setVisible(true);
this.setSize(500, 500);
this.setLocationRelativeTo(null);// 设置居中显示
// 设置Jframe窗体关闭时 程序结束
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
2.2:组合框数据容器的来源
2.2.1:方式一:DefaultComboBoxModel提供数据(推荐)
这种方式数据类型可以死Object类型的
//创建组合框数据容器
DefaultComboBoxModel<Object> ComboBoxModel = new DefaultComboBoxModel<>();
ComboBoxModel.addElement("张三");
ComboBoxModel.addElement("李四");
ComboBoxModel.addElement(123);
//创建组合框
JComboBox<Object> comboBox = new JComboBox<>(ComboBoxModel);
2.2.2:数组的方式提供数据容器
这种方式提供的数据只能是String类型的数据
//创建组合框数据容器
String[] str = new String[]{"张三","李四","王五"};
//创建组合框
JComboBox<Object> comboBox = new JComboBox<>(str);
3:组合框联动(省市二级联动)
3.1:代码实现
package Swing;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class Main9 extends JFrame{
public static void main(String[] args) {
Main9 main9 = new Main9();
main9.run();
}
public void run(){
//创建其他组件
JButton button = new JButton("确定");
button.setBounds(250, 50, 80, 35);
button.setFont(new Font("楷体", Font.CENTER_BASELINE, 15));
//创建数据容器
//创建省
DefaultComboBoxModel<Object> boxModel = new DefaultComboBoxModel<>();
boxModel.addElement("山西");
boxModel.addElement("陕西");
boxModel.addElement("重庆");
//创建市
DefaultComboBoxModel<Object> modelCity1 = new DefaultComboBoxModel<>(new String[]{"太原","大同","吕梁"});
DefaultComboBoxModel<Object> modelCity2 = new DefaultComboBoxModel<>(new String[]{"西安","宝鸡","汉中"});
DefaultComboBoxModel<Object> modelCity3 = new DefaultComboBoxModel<>(new String[]{"渝北","璧山","沙坪坝"});
//创建组合框省级和市级的组合框
JComboBox<Object> jComboBox1 = new JComboBox<>();//省
jComboBox1.setBounds(50, 50, 80, 35);
jComboBox1.setFont(new Font("楷体", Font.CENTER_BASELINE, 15));
JComboBox<Object> jComboBox2 = new JComboBox<>();//市
jComboBox2.setBounds(150, 50, 80, 35);
jComboBox2.setFont(new Font("楷体", Font.CENTER_BASELINE, 15));
//设置jComboBox2默认展示为山西
jComboBox2.setModel(modelCity1); //setModel的数据类型是Object
//添加数据容器到组合框
jComboBox1.setModel(boxModel);
/**
* jComboBox1设置监听器
* */
jComboBox1.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if(e.getItem().equals("山西")){
jComboBox2.setModel(modelCity1);
}else if(e.getItem().equals("陕西")){
jComboBox2.setModel(modelCity2);
}else if(e.getItem().equals("重庆")){
jComboBox2.setModel(modelCity3);
}
}
});
//创建container
Container container = this.getContentPane();
container.add(jComboBox1);
container.add(jComboBox2);
container.add(button);
//设置布局管理器
this.setLayout(null);
/**
* 设置窗体属性
* */
this.setTitle("组合框-省市二级联动");
this.setVisible(true);
this.setSize(500, 500);
this.setLocationRelativeTo(null);// 设置居中显示
// 设置Jframe窗体关闭时 程序结束
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
3.2:效果展示
4:对上述代码的优化
对与上述代码,if ... else if 判断代码台冗余,我们采用List集合存放城市的DefaultComboBoxModel数据容器
4.1:采用List集合存放DefaultComboBoxModel数据容器
4.1.1:本方法结合addItemListener监听器
代码实现
package Swing;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class Main9 extends JFrame{
public static void main(String[] args) {
Main9 main9 = new Main9();
main9.run();
}
public void run(){
//创建其他组件
JButton button = new JButton("确定");
button.setBounds(250, 50, 80, 35);
button.setFont(new Font("楷体", Font.CENTER_BASELINE, 15));
//创建数据容器
//创建省
DefaultComboBoxModel<Object> boxModel = new DefaultComboBoxModel<>();
boxModel.addElement("山西");
boxModel.addElement("陕西");
boxModel.addElement("重庆");
//创建市
DefaultComboBoxModel<Object> modelCity1 = new DefaultComboBoxModel<>(new String[]{"太原","大同","吕梁"});
DefaultComboBoxModel<Object> modelCity2 = new DefaultComboBoxModel<>(new String[]{"西安","宝鸡","汉中"});
DefaultComboBoxModel<Object> modelCity3 = new DefaultComboBoxModel<>(new String[]{"渝北","璧山","沙坪坝"});
//创建集合
List<DefaultComboBoxModel<Object>> list = new ArrayList<DefaultComboBoxModel<Object>>();
list.add(modelCity1);
list.add(modelCity2);
list.add(modelCity3);
//创建组合框省级和市级的组合框
JComboBox<Object> jComboBox1 = new JComboBox<>();//省
jComboBox1.setBounds(50, 50, 80, 35);
jComboBox1.setFont(new Font("楷体", Font.CENTER_BASELINE, 15));
JComboBox<Object> jComboBox2 = new JComboBox<>();//市
jComboBox2.setBounds(150, 50, 80, 35);
jComboBox2.setFont(new Font("楷体", Font.CENTER_BASELINE, 15));
//设置jComboBox2默认展示为山西
jComboBox2.setModel(modelCity1); //setModel的数据类型是Object
//添加数据容器到组合框
jComboBox1.setModel(boxModel);
/**
* jComboBox1设置监听器
* */
jComboBox1.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED){ //判断被选择
int index = jComboBox1.getSelectedIndex();
jComboBox2.setModel(list.get(index));
}
}
});
//创建container
Container container = this.getContentPane();
container.add(jComboBox1);
container.add(jComboBox2);
container.add(button);
//设置布局管理器
this.setLayout(null);
/**
* 设置窗体属性
* */
this.setTitle("组合框-省市二级联动");
this.setVisible(true);
this.setSize(500, 500);
this.setLocationRelativeTo(null);// 设置居中显示
// 设置Jframe窗体关闭时 程序结束
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
4.1.2:采用addActionListener实现
只需用更换一下监听器就可以了
/**
* jComboBox1设置监听器
*/
jComboBox1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int index = jComboBox1.getSelectedIndex();
jComboBox2.setModel(list.get(index));
}
});