JAVA下拉列表框组件

1.JComboBox类

Swing中的下拉列表框使用JComboBox类对象来表示,它是javax.swing.JComponent类的子类。
它的常用构造方法如下:

  1. public JComboBox().
  2. public JComboBox(ComboBoxModel dataModel).
  3. public JComboBox(Object[] arrayData).
  4. public JComboBox(Vector vector).
    在初始化下拉列表框时,可以选择同时指定下拉列表框中的项目内容,也可以在程序中使用其他方法设置下拉列表框中的内容,下拉列表框中的内容可以被封装在ComboBoxModel类型、数组或Vector类型中。
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Demo12 extends JFrame{
	public Demo12() {
		setBounds(100,100,190,120);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		Container c=getContentPane();
		c.setLayout(null);
		JComboBox<String> comboBox=new JComboBox<>();
		comboBox.addItem("身份证");
		comboBox.addItem("学生证");
		comboBox.addItem("工作证");
		JButton btn =new JButton("打印");
		btn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.out.println("选中的索引"+comboBox.getSelectedIndex());
				System.out.println("选中的值"+comboBox.getSelectedItem());
			}
		});
		btn.setBounds(100, 10, 60, 20);
		c.add(btn);
		comboBox.setBounds(10, 10, 80, 21);
		c.add(comboBox);
		setVisible(true);
	}
	public static void main(String[] args) {
		new Demo12();
	}
}
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Demo12 extends JFrame{
	public Demo12() {
		setBounds(100,100,190,120);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		Container c=getContentPane();
		c.setLayout(null);
		String items[] = {"身份证","学生证","军人证"};
		JComboBox<String> comboBox=new JComboBox<>(items);
		JButton btn =new JButton("打印");
		btn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.out.println("选中的索引 "+comboBox.getSelectedIndex());
				System.out.println("选中的值 "+comboBox.getSelectedItem());
			}
		});
		btn.setBounds(100, 10, 60, 20);
		c.add(btn);
		comboBox.setBounds(10, 10, 80, 21);
		c.add(comboBox);
		setVisible(true);
	}
	public static void main(String[] args) {
		new Demo12();
	}
}
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Demo12 extends JFrame{
	public Demo12() {
		setBounds(100,100,190,120);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		Container c=getContentPane();
		c.setLayout(null);
		JComboBox<String> comboBox=new JComboBox<>();
		String items[] = {"身份证","学生证","军人证"};
		ComboBoxModel cm = new DefaultComboBoxModel<>(items);
		comboBox.setModel(cm);
		JButton btn =new JButton("打印");
		btn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.out.println("选中的索引 "+comboBox.getSelectedIndex());
				System.out.println("选中的值 "+comboBox.getSelectedItem());
			}
		});
		btn.setBounds(100, 10, 60, 20);
		c.add(btn);
		comboBox.setBounds(10, 10, 80, 21);
		c.add(comboBox);
		setVisible(true);
	}
	public static void main(String[] args) {
		new Demo12();
	}
}

你可能感兴趣的:(JAVA下拉列表框组件)