eclipse swt 程序解读


作者: beauty9235  链接: http://beauty9235.javaeye.com/blog/229627  发表时间: 2008年07月23日

声明:本文系JavaEye网站发布的原创博客文章,未经作者书面许可,严禁任何网站转载本文,否则必将追究法律责任!

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class DogShowRegistrationWindow {
	/*
	 * 参考文章
	 * http://www.eclipse.org/articles/article.php?file=Article-Understanding-Layouts/index.html
	 * http://www.ibm.com/developerworks/cn/linux/opensource/os-ecgui1/index.html
	 * http://www.ibm.com/developerworks/cn/linux/opensource/os-ecgui2/index.html
	 * http://www.ibm.com/developerworks/cn/linux/opensource/os-ecgui3/index.html
	 * http://www-128.ibm.com/developerworks/cn/opensource/os-swingswt/
	 * http://www.blogjava.net/fortune/archive/2006/03/09/34463.html
	 */	
	Image dogImage;
	Text dogNameText;
	Combo dogBreedCombo;
	Canvas dogPhoto;
	List categories;
	Text nameText;
	Text phoneText;

	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new DogShowRegistrationWindow().createShell(display);
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}
	
	public Shell createShell(final Display display) {
		final Shell shell = new Shell(display);//通用窗口 
		FormLayout layout = new FormLayout();//布局管理器 
		layout.marginWidth = 5;
		layout.marginHeight = 5;
		shell.setLayout(layout);
		shell.setText("Dog Show Entry");
	    //****************************定义布局所要用的资源开始****************************
		//定义dog name标签
		Label dogName = new Label(shell, SWT.NONE);//显示静态文本 
		dogName.setText("Dog's Name:");
		//定义dog name的文本输入框
		dogNameText = new Text(shell, SWT.SINGLE | SWT.BORDER);//文本输入 
		//定义dog greed标签
		Label dogBreed = new Label(shell, SWT.NONE);
		dogBreed.setText("Breed:");
		//定义一个dog greed多选框
		dogBreedCombo = new Combo(shell, SWT.NONE);
		dogBreedCombo.setItems(new String[] { "Collie", "Pitbull", "Poodle","Scottie", "Black Lab" });//从一个下拉列表中选择
		//定义photo标签
		Label photo = new Label(shell, SWT.NONE);
		photo.setText("Photo:");
		//定义photo显示域
		dogPhoto = new Canvas(shell, SWT.BORDER);//绘图区域;可能用于定制控件 
		//定义Photo Browse按钮
		Button browse = new Button(shell, SWT.PUSH);
		browse.setText("Browse...");
		//定义Photo Delete按钮
		Button delete = new Button(shell, SWT.PUSH);//简单按下具有文本的按钮 
		delete.setText("Delete");
		//定义分类标签
		Label cats = new Label(shell, SWT.NONE);
		cats.setText("Categories");
		//定义分类List
		categories = new List(shell, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);//从条目列表中进行选择 
		categories.setItems(new String[] { "Best of Breed", "Prettiest Female",
				"Handsomest Male", "Best Dressed", "Fluffiest Ears",
				"Most Colors", "Best Performer", "Loudest Bark",
				"Best Behaved", "Prettiest Eyes", "Most Hair", "Longest Tail",
				"Cutest Trick" });
		//定义一个组
		Group ownerInfo = new Group(shell, SWT.NONE);//其他控件的有边界和标题的通用容器 
		ownerInfo.setText("Owner Info");
		FormLayout ownerLayout = new FormLayout();//布局管理器 
		ownerLayout.marginWidth = 5;
		ownerLayout.marginHeight = 5;
		ownerInfo.setLayout(ownerLayout);
		
		//定义Enter按钮
		Button enter = new Button(shell, SWT.PUSH);
		enter.setText("Enter");
		//****************************定义布局所要用的资源结束****************************
		FormData data = new FormData();
		data.top = new FormAttachment(dogNameText, 0, SWT.CENTER);
		dogName.setLayoutData(data);//定义dogName与dogNameText的中心位置对齐
		
		data = new FormData();
		data.left = new FormAttachment(dogName, 5);
		data.right = new FormAttachment(100, 0);
		dogNameText.setLayoutData(data);//定义dogNameText左边距dogName5个点数的偏移  右边100%填充
		
		data = new FormData();
		data.top = new FormAttachment(dogBreedCombo, 0, SWT.CENTER);
		dogBreed.setLayoutData(data);
		//定义dogBreed项部距dogBreedCombo 0个点数的偏移 与dogBreedCombo标签的中心位置对齐 
		
		data = new FormData();
		data.top = new FormAttachment(dogNameText, 5);
		data.left = new FormAttachment(dogNameText, 0, SWT.LEFT);
		data.right = new FormAttachment(categories, -5);
		dogBreedCombo.setLayoutData(data);
		//定义dogBreedCombo项部距dogNameText 5个点数的偏移
		//定义dogBreedCombo左边与 dogNameText的左边对齐
		//定义dogBreedCombo右边距 categories左边5个点数的偏移
		
		data = new FormData(80, 80);
		data.top = new FormAttachment(dogBreedCombo, 5);
		data.left = new FormAttachment(dogNameText, 0, SWT.LEFT);
		data.right = new FormAttachment(categories, -5);
		data.bottom = new FormAttachment(ownerInfo, -5);
		dogPhoto.setLayoutData(data);
		//定义dogPhoto大小为80X80
		//定义dogPhoto顶边与dogBreedCombo底边点数的偏移
		//定义dogPhoto左边与dogNameText左边对齐
		//定义dogPhoto右边距categories 5个点数的偏移
		//定义dogPhoto底边距ownerInfo 5个点数的偏移
		dogPhoto.addPaintListener(new PaintListener() {
			public void paintControl(final PaintEvent event) {
				if (dogImage != null) {
					event.gc.drawImage(dogImage, 0, 0);
				}
			}
		});
		
		data = new FormData();
		data.top = new FormAttachment(dogPhoto,0, SWT.TOP);
		photo.setLayoutData(data);
		//定义photo顶边与 dogPhoto的顶边对齐
		
		data = new FormData();
		data.top = new FormAttachment(photo, 5);
		data.right = new FormAttachment(dogPhoto, -5);
		browse.setLayoutData(data);
		//定义photo顶边距 photo 5个点数的偏移
		//定义photo右边距 dogPhoto 5个点数的偏移
		browse.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				String fileName = new FileDialog(shell).open();//用来显示一个文件对话
				if (fileName != null) {
					dogImage = new Image(display, fileName);
				}
			}
		});
		
		data = new FormData();
		data.top = new FormAttachment(browse, 5);
		data.left = new FormAttachment(browse, 0, SWT.LEFT);
		data.right = new FormAttachment(dogPhoto, -5);
		delete.setLayoutData(data);
		//定义delete顶边距 browse 5个点数的偏移
		//定义delete左边与 browse 左边对齐
		//定义delete右边距 dogPhoto 5个点数的偏移
		delete.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				if (dogImage != null) {
					dogImage.dispose();
					dogImage = null;
					dogPhoto.redraw();
				}
			}
		});
		
		data = new FormData(90, 140);
		data.top = new FormAttachment(dogPhoto, 0, SWT.TOP);
		data.right = new FormAttachment(100, 0);
		data.bottom = new FormAttachment(enter, -5);
		categories.setLayoutData(data);
		//定义categories顶边与 dogPhoto的顶边对齐
		//定义categories右边100% 填充
		//定义categories底边距底边父组件 5个点数的偏移 
		
		data = new FormData();
		data.bottom = new FormAttachment(categories, -5);
		data.left = new FormAttachment(categories, 0, SWT.CENTER);
		cats.setLayoutData(data);
		//定义cats底边与 categories的底边 5个点数的偏移
		//定义cats左边与 categories 的中心对齐
		
		data = new FormData();
		data.right = new FormAttachment(100, 0);
		data.bottom = new FormAttachment(100, 0);
		enter.setLayoutData(data);
		enter.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				System.out.println("\nDog Name: " + dogNameText.getText());
				System.out.println("Dog Breed: " + dogBreedCombo.getText());
				System.out.println("Owner Name: " + nameText.getText());
				System.out.println("Owner Phone: " + phoneText.getText());
				System.out.println("Categories:");
				String cats[] = categories.getSelection();
				for (int i = 0; i < cats.length; i++) {
					System.out.println("\t" + cats[i]);
				}
			}
		});
		
		data = new FormData();
		data.bottom = new FormAttachment(enter, -5);
		data.left = new FormAttachment(0, 0);
		data.right = new FormAttachment(categories, -5);
		ownerInfo.setLayoutData(data);
		
		Label name = new Label(ownerInfo, SWT.NULL);
		name.setText("Name:");
		
		Label phone = new Label(ownerInfo, SWT.PUSH);
		phone.setText("Phone:");
		
		nameText = new Text(ownerInfo, SWT.SINGLE | SWT.BORDER);
		phoneText = new Text(ownerInfo, SWT.SINGLE | SWT.BORDER);
		
		data = new FormData();
		data.top = new FormAttachment(nameText, 0, SWT.CENTER);
		name.setLayoutData(data);
		
		data = new FormData();
		data.top = new FormAttachment(phoneText, 0, SWT.CENTER);
		phone.setLayoutData(data);
		
		data = new FormData();
		data.left = new FormAttachment(phone, 5);
		data.right = new FormAttachment(100, 0);
		nameText.setLayoutData(data);
		
		data = new FormData();
		data.left = new FormAttachment(nameText, 0, SWT.LEFT);
		data.right = new FormAttachment(100, 0);
		data.top = new FormAttachment(55, 0);
		phoneText.setLayoutData(data);
		
		shell.pack();
		
		return shell;
	}	
}

本文的讨论也很精彩,浏览讨论>>


JavaEye推荐
  • 搜狐网站诚聘Java、PHP和C++工程师
  • 北京: 千橡集团暨校内网诚聘软件研发工程师





欢迎大家访问我的个人网站 萌萌的IT人

你可能感兴趣的:(eclipse swt 程序解读)