SWT基礎學習

開發步驟:

1.創建新工程,在src下新建lib文件夾,然後導入需要的jar包,配置 Java Build Path 將剛剛複製過來的jar包導入到工程,

                  

2.新建一個java類,並創建main方法,方法內寫入:

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;


public class TestShell {
	public static void main(String[] args) {
		/**
		 * 第一步創建Display,對應操作系統的控件,使用完必須釋放
		 */
		Display display = new Display();
		/**
		 * 第二部創建shell
		 * @style 
		 */
		Shell shell = new Shell(display);
		shell.setText("第一個shell窗口");
		/**
		 * 第三部指定容器的佈局類型
		 */
		GridLayout gl = new GridLayout(2,false);
		gl.marginBottom = 20;
		gl.marginTop = 10;
		gl.marginLeft=30;
		gl.marginRight = 30;
		gl.verticalSpacing = 20;
		gl.horizontalSpacing = 20;
		shell.setLayout(gl);
		/****
		 * 第四步創建容器裏的控件,並指定擺放位置
		 */
		Label label1 = new Label(shell,SWT.NONE);
		label1.setText("姓名");
		/**
		 * 第一個參數是操作系統資源
		 * 第二參數是字體樣式
		 * 第三參數是字的高度(字號大小)
		 * 第4個參數是字顯示樣式
		 */
		Font labelFont = new Font(display,"微軟雅黑",20,SWT.NONE);
		
		Text text1 = new Text(shell,SWT.BORDER);
		text1.setFont(labelFont);
		text1.setLayoutData(new GridData(SWT.FILL,SWT.TOP,true,false));
		
		Label label2 = new Label(shell,SWT.NONE);
		label2.setText("密碼");
		
		Color foreColor = new Color(display,255,0,0);
		label2.setForeground(foreColor);
		
		Text text2 = new Text(shell,SWT.BORDER|SWT.PASSWORD);
		text2.setLayoutData(new GridData(SWT.FILL,SWT.TOP,true,false));
		Label label3 = new Label(shell,SWT.NONE);
		label3.setText("下拉:");
		Combo combo = new Combo(shell,SWT.NONE);
		for(int i=1; i<=9; i++){
			combo.add("下拉選項"+i);
		}
		combo.setLayoutData(new GridData(SWT.FILL,SWT.CENTER,true,false));
		
		/**
		 * 創建表格4列的表格
		 * 
		 * 1.創建表頭
		 * 2.創建內容數據
		 */
		//表格樣式是多選 垂直 水平滾動條,顯示邊緣
		Table table = new Table(shell,SWT.MULTI|SWT.V_SCROLL|SWT.H_SCROLL|SWT.BORDER);
		table.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true,2,1));
		table.setHeaderVisible(true);//設置表格頭可見
		table.setLinesVisible(true);//設置表格線可見
		//創建表格頭
		for(int i=1; i<=4; i++){
			TableColumn tableColumn = new TableColumn(table,SWT.NONE);
			tableColumn.setText("第"+i+"列");
			tableColumn.setWidth(200);
		}
		
		//創建幾個數據
		TableItem item1 = new TableItem(table,SWT.NONE);
		item1.setText(new String[]{"111","222","3333","444"});
		
		Button btn = new Button(shell,SWT.NONE);
		btn.setText("提交");
		btn.setLayoutData(new GridData(SWT.CENTER,SWT.CENTER,false,false,2,1));
		
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		
		
		display.dispose();
	
	}
}


至此,一個簡單的shell就創建成功了.運行結果如下:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章