swing-两种下拉框

1.Choice

import java.awt.Choice;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class checboxTest extends JPanel {
	static JFrame frame = new JFrame();
	static JPanel panel = new JPanel();
	
	public checboxTest() {
		
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		checboxTest();
	}
	private static void checboxTest() {
		// TODO Auto-generated method stub
		frame.setPreferredSize(new Dimension(400, 300));
		Choice jc = new Choice();
		// 添加文件的键付给JTextField的name属性。
		List<String> items = new ArrayList<String>();
		// list里面添加默认值
		items.add("\"image/png\"");
		items.add("\"image/gif\"");
		items.add("\"image/jpg\"");
		for (int i = 0; i < items.size(); i++) {
			jc.add(items.get(i));
		}
		System.out.println("-----++--" + jc.getSelectedIndex());// 取得所选的下拉框,文件索引
		System.out.println("-----222++--" + jc.getSelectedItem());// 取得下拉框文件内容
		JLabel label = new JLabel("测试");
		panel.add(label);
		panel.add(jc);
		frame.add(panel);
		frame.pack();
		frame.setVisible(true);
	}
}

 

2.JCheckbox‘

import java.awt.Choice;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class checboxTest extends JPanel {
	static JFrame frame = new JFrame();
	static JPanel panel = new JPanel();
	
	public checboxTest() {
		
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		checboxTest();
	}
	private static void checboxTest() {
		// TODO Auto-generated method stub
		frame.setPreferredSize(new Dimension(400, 300));
	    DefaultComboBoxModel model = new DefaultComboBoxModel();
        model.addElement("自动化");
        model.addElement("自动化1");
        JComboBox comboBox = new JComboBox(model);
        System.out.println(model.getSize());//获得model大小
		System.out.println("-----222++--" + comboBox.getSelectedItem());// 取得下拉框文件内容
		JLabel label = new JLabel("测试");
		panel.add(label);
		panel.add(comboBox);
		frame.add(panel);
		frame.pack();
		frame.setVisible(true);
	}
}

 

你可能感兴趣的:(swing)