您的位置:首页 > 其它

j2se的几种常用组件例子

2015-03-03 15:58 176 查看
public class my_element extends JFrame{
	//JFrame可用的界面组件
	JPanel jp1 = new JPanel();	//panel
	JPanel jp2 = new JPanel();
	JButton jb = new JButton("按钮");
	//JButton jb = new JButton(new ImageIcon("图片地址"));按钮为这个图片
	//jb.setToolTipText("鼠标放在按钮上时的tip提示内容");
	JLabel jlab = new JLabel("标签");
	JTextField jtext = new JTextField(10);	//数字为长度
	JPasswordField jpass = new JPasswordField(10);
	//复选框
	JCheckBox jcheck1 = new JCheckBox("复选框1");
	JCheckBox jcheck2 = new JCheckBox("复选框2");
	JCheckBox jcheck3 = new JCheckBox("复选框3");
	//单选按钮	
	JRadioButton jradio1 = new JRadioButton("单选框1");
	JRadioButton jradio2 = new JRadioButton("单选框2");
	JRadioButton jradio3 = new JRadioButton("单选框3");
	//下拉框
	String[] com = {"北京","天津","火星"};
	JComboBox jbox = new JComboBox(com);
	//列表
	String[] list = {"北京","天津","火星"};
	JList jlist = new JList(list);
	//滚动面板
	JScrollPane jsp = new JScrollPane(jlist);
	
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		my_element me = new my_element();
	}
	
//	构造函数
	public my_element(){
		
		//设置布局管理
		this.setLayout(new GridLayout(3,1));
	
		//加入组件
		jp1.add(jlab);
		jp1.add(jtext);
		jp1.add(jpass);
		jp1.add(jb);
		jp1.add(jcheck1);
		jp1.add(jcheck2);
		jp1.add(jcheck3);
		jp1.add(jradio1);
		jp1.add(jradio2);
		//创建buttongroup
		ButtonGroup group = new ButtonGroup();	//单选框需要先创建buttongroup
		group.add(jradio1);
		group.add(jradio2);
		group.add(jradio3);
		jp1.add(jradio1);
		jp1.add(jradio2);
		jp1.add(jradio3);
		
		jp2.add(jbox);
		//设置list最大显示数
		jlist.setVisibleRowCount(2);
		jp2.add(jsp);

		//把panel放入JFrame
		this.add(jp1);
		this.add(jp2);
		
		//给窗体设置标题  
        this.setTitle("控件组合");  
        //设置大小  
        this.setSize(640, 240);  
      
          
        //设置初始位置  
        this.setLocation(100, 200);  
          
        //设置当关闭窗口时jvm也退出。  
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        //显示  
        this.setVisible(true);  
	}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: