[的Java Swing] 的JComboBox在Java中 – 的JComboBox在Java中

内容
如何创建一个JComboBox,
获取事件JComboBox的

JComboBox可一个对象可以让你选择一个元素上的箭头,当点击. 例如,我们有一个 的JFrame 从以下的JComboBox选择标志:

的JComboBox在Java中

现在,我们将创建一个JFrame的JComboBox像学习.

如何创建一个JComboBox,

要创建JComboBox中,我们使用下面的构造函数:
– 的JComboBox(): 创建无数据的JComboBox
– 的JComboBox(ComboBoxModel中AMODEL): 创建模型的JComboBox与指定
– 的JComboBox(和[] 项目): 与阵列元件E创建的JComboBox
– 的JComboBox(矢量项目): 在矢量E中的元素创建的JComboBox

程序将创建类似下面的第一篇文章的JFrame. 它包含一个主面板 的JPanel. 此放置在主面板 BorderLayout的 和含有水的JComboBox选择PAGE_START, 的JLabel 选定的国家的国旗显示设置为CENTER. 在所使用的代码 2 String数组arrFlag命名与名字和arrCountry国家标志来显示该国在JComboBox的名字.

package nguyenvanquan7826.JComboBox;

import java.awt.BorderLayout;

import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class MyJComboBox extends JFrame {
	private JComboBox<String> cbbCountry;
	private JLabel lbShow;
	private String[] arrFlag = { "vn", "china", "us", "lao", "en" };
	private String[] arrCountry = { "Việt Nam", "Trung Quốc", "Mỹ", "Lào",
			"Anh" };

	public MyJComboBox() {
		// add main panel to JFrame
		add(createMainPanel());
		// set Display
		setTitle("JComboBox");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(300, 300);
		setLocationRelativeTo(null);
		setVisible(true);
	}

	// create main panel with BorderLayout
	private JPanel createMainPanel() {
		JPanel panel = new JPanel(new BorderLayout());
		panel.setBorder(new EmptyBorder(10, 10, 10, 10));
		// add a JComboBox to top of main panel
		panel.add(cbbCountry = createComboBox(arrCountry),
				BorderLayout.PAGE_START);
		// get flag name with country is selected in JComboBox
		String flag = arrFlag[cbbCountry.getSelectedIndex()];
		// add JLabel to show flag
		panel.add(lbShow = createLabel(flag), BorderLayout.CENTER);
		return panel;
	}

	// create a JComboBox with list item in ArrayList
	private JComboBox<String> createComboBox(String[] arrItem) {
		JComboBox<String> cbb = new JComboBox<String>(arrItem);
		return cbb;
	}

	// create JLabel to show flag with
	private JLabel createLabel(String flagName) {
		JLabel lb = new JLabel(getFlag(flagName));
		return lb;
	}

	// get flag with name is flagName
	private ImageIcon getFlag(String flagName) {
		ImageIcon flag = new ImageIcon(getClass().getResource(
				"/nguyenvanquan7826/JComboBox/" + flagName + ".png"));
		return flag;
	}

	public static void main(String[] args) {
		new MyJComboBox();
	}
}

JComboBox的初始化时的默认, 如果在它的任何元件时,第一元件被选择. 要确定哪些元件被选择,我们可以使用该方法 getSelectedIndex() (取位置元素被选中) 或 获得当选项目() (取元件被选择) 这取决于如何和不同的目的. 你有没有注意到拍照时,有必要按照路径您的影像修订, 他的照片放在一起包用java文件*. (下载照片这里)

的JComboBox在Java中

你有没有注意到,在上面的代码没有捕获的事件在其他国家选择的元素的JComboBox,选择标志时,并没有改变.

获取事件JComboBox的

为了能够在JComboBox中选择元素时,我们需要赶上事件 addActionListener方法 町的JComboBox. 同时我们要 implemets接口的ActionListener 和压倒一切的方法 为actionPerformed(ActionEvent的ê). 然后,我们将比较的方法来识别和处理的影响的JComboBox. 我们有完整的代码如下:

package nguyenvanquan7826.JComboBox;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class MyJComboBox extends JFrame implements ActionListener {
	private JComboBox<String> cbbCountry;
	private JLabel lbShow;
	private String[] arrFlag = { "vn", "china", "us", "lao", "en" };
	private String[] arrCountry = { "Việt Nam", "Trung Quốc", "Mỹ", "Lào",
			"Anh" };

	public MyJComboBox() {
		// add main panel to JFrame
		add(createMainPanel());
		// set Display
		setTitle("JComboBox");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(300, 300);
		setLocationRelativeTo(null);
		setVisible(true);
	}

	// create main panel with BorderLayout
	private JPanel createMainPanel() {
		JPanel panel = new JPanel(new BorderLayout());
		panel.setBorder(new EmptyBorder(10, 10, 10, 10));
		// add a JComboBox to top of main panel
		panel.add(cbbCountry = createComboBox(arrCountry),
				BorderLayout.PAGE_START);
		// get flag name with country is selected in JComboBox
		String flag = arrFlag[cbbCountry.getSelectedIndex()];
		// add JLabel to show flag
		panel.add(lbShow = createLabel(flag), BorderLayout.CENTER);
		return panel;
	}

	// create a JComboBox with list item in ArrayList
	private JComboBox<String> createComboBox(String[] arrItem) {
		JComboBox<String> cbb = new JComboBox<String>(arrItem);
		cbb.addActionListener(this);
		return cbb;
	}

	// create JLabel to show flag with
	private JLabel createLabel(String flagName) {
		JLabel lb = new JLabel(getFlag(flagName));
		return lb;
	}

	// get flag with name is flagName
	private ImageIcon getFlag(String flagName) {
		ImageIcon flag = new ImageIcon(getClass().getResource(
				"/nguyenvanquan7826/JComboBox/" + flagName + ".png"));
		return flag;
	}

	// change flag when select items in JComboBox
	private void changeFlag() {
		lbShow.setIcon(getFlag(arrFlag[cbbCountry.getSelectedIndex()]));
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == cbbCountry) {
			changeFlag();
		}
	}

	public static void main(String[] args) {
		new MyJComboBox();
	}
}

阅读更多: JComboBox的类, 使用的JComboBox