欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java简单应用----下拉条的 应用

程序员文章站 2024-03-24 12:20:28
...
package zuoye0517;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class XIA extends JFrame{
	private JComboBox lst;
	private JLabel lbl;
	private Object cities[]={"","叫哥哥","叫爸爸"};
	public XIA(){
		super("dadadadad");
		Container c=getContentPane();
		c.setLayout(new FlowLayout());
		lst=new JComboBox(cities);
		lst.setMaximumRowCount(4);
		lbl=new JLabel("请从组合框中选择");
		c.add(lst);
		c.add(lbl);
		lst.addItemListener(new Handler1());
		setSize(300,150);
		setVisible(true);
	}
	public static void main(String args[]){
		XIA app=new XIA();
		app.addWindowListener( new Handler2());
	}
	class Handler1 implements ItemListener{
		public void itemStateChanged(ItemEvent e){
			lbl.setText("你选中了:"+lst.getSelectedItem());
		}
	}
	static class Handler2 extends WindowAdapter{
		public void windowClosing(WindowEvent e){
			System.exit(0);
		}
	}
	

}
一个简单而容易理解的例子  ,需要注意的是监听设置。
相关标签: java