作者: beauty9235 鏈接:http://beauty9235.javaeye.com/blog/229627 發(fā)表時(shí)間: 2008年07月23日
聲明:本文系JavaEye網(wǎng)站發(fā)布的原創(chuàng)博客文章,未經(jīng)作者書面許可,嚴(yán)禁任何網(wǎng)站轉(zhuǎn)載本文,否則必將追究法律責(zé)任!
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.aygfsteel.com/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標(biāo)簽 Label dogName = new Label(shell, SWT.NONE);//顯示靜態(tài)文本 dogName.setText("Dog's Name:"); //定義dog name的文本輸入框 dogNameText = new Text(shell, SWT.SINGLE | SWT.BORDER);//文本輸入 //定義dog greed標(biāo)簽 Label dogBreed = new Label(shell, SWT.NONE); dogBreed.setText("Breed:"); //定義一個(gè)dog greed多選框 dogBreedCombo = new Combo(shell, SWT.NONE); dogBreedCombo.setItems(new String[] { "Collie", "Pitbull", "Poodle","Scottie", "Black Lab" });//從一個(gè)下拉列表中選擇 //定義photo標(biāo)簽 Label photo = new Label(shell, SWT.NONE); photo.setText("Photo:"); //定義photo顯示域 dogPhoto = new Canvas(shell, SWT.BORDER);//繪圖區(qū)域;可能用于定制控件 //定義Photo Browse按鈕 Button browse = new Button(shell, SWT.PUSH); browse.setText("Browse..."); //定義Photo Delete按鈕 Button delete = new Button(shell, SWT.PUSH);//簡(jiǎn)單按下具有文本的按鈕 delete.setText("Delete"); //定義分類標(biāo)簽 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);//從條目列表中進(jìn)行選擇 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" }); //定義一個(gè)組 Group ownerInfo = new Group(shell, SWT.NONE);//其他控件的有邊界和標(biāo)題的通用容器 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"); //****************************定義布局所要用的資源結(jié)束**************************** FormData data = new FormData(); data.top = new FormAttachment(dogNameText, 0, SWT.CENTER); dogName.setLayoutData(data);//定義dogName與dogNameText的中心位置對(duì)齊 data = new FormData(); data.left = new FormAttachment(dogName, 5); data.right = new FormAttachment(100, 0); dogNameText.setLayoutData(data);//定義dogNameText左邊距dogName5個(gè)點(diǎn)數(shù)的偏移 右邊100%填充 data = new FormData(); data.top = new FormAttachment(dogBreedCombo, 0, SWT.CENTER); dogBreed.setLayoutData(data); //定義dogBreed項(xiàng)部距dogBreedCombo 0個(gè)點(diǎn)數(shù)的偏移 與dogBreedCombo標(biāo)簽的中心位置對(duì)齊 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項(xiàng)部距dogNameText 5個(gè)點(diǎn)數(shù)的偏移 //定義dogBreedCombo左邊與 dogNameText的左邊對(duì)齊 //定義dogBreedCombo右邊距 categories左邊5個(gè)點(diǎn)數(shù)的偏移 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底邊點(diǎn)數(shù)的偏移 //定義dogPhoto左邊與dogNameText左邊對(duì)齊 //定義dogPhoto右邊距categories 5個(gè)點(diǎn)數(shù)的偏移 //定義dogPhoto底邊距ownerInfo 5個(gè)點(diǎn)數(shù)的偏移 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的頂邊對(duì)齊 data = new FormData(); data.top = new FormAttachment(photo, 5); data.right = new FormAttachment(dogPhoto, -5); browse.setLayoutData(data); //定義photo頂邊距 photo 5個(gè)點(diǎn)數(shù)的偏移 //定義photo右邊距 dogPhoto 5個(gè)點(diǎn)數(shù)的偏移 browse.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { String fileName = new FileDialog(shell).open();//用來(lái)顯示一個(gè)文件對(duì)話 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個(gè)點(diǎn)數(shù)的偏移 //定義delete左邊與 browse 左邊對(duì)齊 //定義delete右邊距 dogPhoto 5個(gè)點(diǎn)數(shù)的偏移 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的頂邊對(duì)齊 //定義categories右邊100% 填充 //定義categories底邊距底邊父組件 5個(gè)點(diǎn)數(shù)的偏移 data = new FormData(); data.bottom = new FormAttachment(categories, -5); data.left = new FormAttachment(categories, 0, SWT.CENTER); cats.setLayoutData(data); //定義cats底邊與 categories的底邊 5個(gè)點(diǎn)數(shù)的偏移 //定義cats左邊與 categories 的中心對(duì)齊 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推薦