常用窗口小部件使用实例

package com.test.cxm.plugin01.perspective;


import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

public class HelloWorldSWT {
	
	public static void main(String args[]){
	    Display display=Display.getDefault();
	    Shell shell=new Shell();
		shell.setSize(327,253);
		shell.setText("mmm");
		shell.setLayout(new FillLayout());
		
		//创建文本框
		final Text text=new Text(shell,SWT.MULTI);
		//创建列表
		final List list=new List(shell,SWT.SINGLE);
		list.setItems(new String[]{"1","2","3"});
		//创建只读下拉框
		final Combo combo1=new Combo(shell,SWT.READ_ONLY);
		combo1.setItems(new String[]{"String1","string2","string3"});
		combo1.setText("string2");
		//创建可编辑的下拉框
		final Combo combo2=new Combo(shell,SWT.DROP_DOWN);
		combo2.setItems(new String[]{"String1","string2","string3"});
		
		//创建表格
		final Table table=new Table(shell,SWT.SINGLE|SWT.BORDER|SWT.SELECTED);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);//默认没有表格线,true的话可以看到
		TableColumn c1=new TableColumn(table, SWT.NULL);
		c1.setText("name");
		c1.pack();
		TableColumn c2=new TableColumn(table, SWT.NULL);
		c2.setText("Age");
		c2.pack();
		TableItem item1=new TableItem(table, SWT.NULL);
		item1.setText(new String[]{"cccc","25"});
		TableItem item2=new TableItem(table, SWT.NULL);
		item2.setText(new String[]{"rrrr","26"});
		//创建树
		//SWT.SINGLE 创建一颗单选树
		//SWT.MULTI 创建一个多选树
		//SWT.CHECK 创建一个单选框树
		final Tree tree=new Tree(shell,SWT.CHECK);
		for(int i=1;i<4;i++){
			TreeItem grandP=new TreeItem(tree, 0);
			grandP.setText("cxm"+i);
			for(int j=1;i<4;i++){
				TreeItem parent=new TreeItem(grandP, 0);
				parent.setText("son"+j);
				for(int k=1;k<4;k++){
					TreeItem child=new TreeItem(parent, 0);
					child.setText("child"+j);
				}
			}
		}
		
	    //复合部件
		//它可以作为其他小部件的容器
		//SWT.BORDER 创建一个具有边框的复合部件
		//SWT.NO_RADIO_GROUP 组件子单选按钮的行为
		//SWT.H_SCROLL 创建一个具有水平滚动条的复合组件
		//SWT.V_SCROLL 创建一个具有垂直滚动条的复合组件
		Composite com=new Composite(shell, SWT.BORDER);
		final Button bn=new Button(com, SWT.PUSH);
		bn.setBounds(25, 25, 50, 80);
		bn.setText("sss");
		
		//组group,是一种特殊的复合组件,它用蚀刻的边框和可选的标签包围了子部件
		//每个组件都包含在组的边框内,并相对应组自我调整大小
		Group group=new Group(shell,SWT.NULL);
		group.setText("group");
		final Button bns=new Button(group, SWT.PUSH);
		bns.setBounds(25, 25, 800, 50);
		bns.setText("hahah");
		
		
		//选项卡文件夹,用于在一个窗口内把信息组织为多个页面
		//点击tab标签,可以使该页面置于前端,tab可以包含图片和文本
		
		final TabFolder tabf=new TabFolder(shell, SWT.BORDER);
		for(int i=1;i<4;i++){
			TabItem ti=new TabItem(tabf, SWT.NULL);
			ti.setText("tab"+i);
			Composite cm=new Composite(tabf, SWT.NULL);//每个便签页面都有个复合组件
			ti.setControl(cm);
			Button bnss=new Button(cm, SWT.PUSH);
			bnss.setBounds(25, 25,80, 50);
			bnss.setText("sss"+i);
		}
		
		//菜单
		//SWT.CASCADE 创建一个带有子菜单的级联菜单
		//SWT.PUST 创建一个标准的菜单
		//SWT.SEPARATOR 创建一个分割线
		Menu menu =new Menu(shell,SWT.BAR);
		shell.setMenuBar(menu);
		MenuItem mi=new MenuItem(menu,SWT.CASCADE);
		mi.setText("sss");
		Menu subM=new Menu(shell,SWT.DROP_DOWN);
                //以最后设置的menu为准
		mi.setMenu(subM);
		MenuItem mi2=new MenuItem(subM, SWT.NULL);
		mi2.setText("eee");
		mi2.setAccelerator(SWT.CTRL+'S');
		MenuItem sep=new MenuItem(subM, SWT.SEPARATOR);
		MenuItem ex=new MenuItem(subM, SWT.NULL);
		ex.setText("exit");
		
		shell.open();
		while(!shell.isDisposed()){
			if(!display.readAndDispatch()){
				display.sleep();
			}
		}
		display.dispose();
		
	}

}
 

你可能感兴趣的:(实例)