SWT界面佈局介紹之FillLayout

給控件設置FillLayout會使子控件充滿父控件的整個空間。FillLayout是SWT佈局中最簡單的一個,它將全部窗口組件以相同尺寸放置到一行或者一列中,不能換行,也不能自定義邊框和距離。下面用一個簡單模擬的工具箱實例來演示。 java

注意:默認狀況下設置佈局爲FillLayout是橫向排列的,在建立佈局時構造函數傳入佈局樣式便可顯式聲明佈局排列方式:new FillLaout(SWT.HORIZONTAL)  new FillLaout(SWT.VERTICAL) shell

截圖:eclipse

代碼:函數

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
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;

/**
 * SWT FillLayout佈局使用demo 簡易信息管理
 * @author xwalker
 *
 */
public class FillLayoutDemo{
	private Shell shell;
	private Table dataTable;
	private Text nameText;
	
	public void open() {
		Display display = Display.getDefault();
		createContents();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}
	/**
	 * 建立窗口組件
	 */
	protected void createContents(){
		shell=new Shell(SWT.MIN|SWT.CLOSE);
		shell.setText("簡易信息管理");
		shell.setSize(300, 576);
		shell.setLayout(new FillLayout(SWT.VERTICAL));
		
		Composite compositeUp = new Composite(shell, SWT.NONE);
		compositeUp.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		dataTable = new Table(compositeUp, SWT.BORDER | SWT.FULL_SELECTION);
		dataTable.setHeaderVisible(true);
		dataTable.setLinesVisible(true);
		
		TableColumn tblclmnId = new TableColumn(dataTable, SWT.NONE);
		tblclmnId.setWidth(39);
		tblclmnId.setText("ID");
		
		TableColumn tblclmnName = new TableColumn(dataTable, SWT.NONE);
		tblclmnName.setWidth(100);
		tblclmnName.setText("Name");
		
		TableColumn tblclmnSex = new TableColumn(dataTable, SWT.NONE);
		tblclmnSex.setWidth(100);
		tblclmnSex.setText("Sex");
		
		TableColumn tblclmnAge = new TableColumn(dataTable, SWT.NONE);
		tblclmnAge.setWidth(100);
		tblclmnAge.setText("Age");
		
		Composite compositeDoen = new Composite(shell, SWT.NONE);
		compositeDoen.setLayout(null);
		
		Group group = new Group(compositeDoen, SWT.NONE);
		group.setText("新增");
		group.setBounds(10, 10, 274, 254);
		group.setLayout(null);
		
		Button cancelBtn = new Button(group, SWT.NONE);
		cancelBtn.setBounds(166, 198, 61, 27);
		cancelBtn.setText("重置");
		
		Button addBtn = new Button(group, SWT.NONE);
		addBtn.setBounds(66, 198, 61, 27);
		addBtn.setText("新增");
		
		Label birthdayLabel = new Label(group, SWT.NONE);
		birthdayLabel.setBounds(10, 123, 53, 17);
		birthdayLabel.setText("Birthday");
		
		DateTime birthdayDate = new DateTime(group, SWT.BORDER);
		birthdayDate.setBounds(85, 123, 95, 24);
		
		Label sexLabel = new Label(group, SWT.NONE);
		sexLabel.setBounds(10, 77, 53, 17);
		sexLabel.setText("Sex");
		
		Button sexMRadio = new Button(group, SWT.RADIO);
		sexMRadio.setBounds(85, 77, 42, 17);
		sexMRadio.setText("男");
		
		Button sexFmRadio = new Button(group, SWT.RADIO);
		sexFmRadio.setBounds(138, 77, 42, 17);
		sexFmRadio.setText("女");
		
		nameText = new Text(group, SWT.BORDER);
		nameText.setBounds(85, 33, 179, 23);
		
		Label nameLabel = new Label(group, SWT.NONE);
		nameLabel.setBounds(10, 33, 53, 17);
		nameLabel.setText("Name");
		insertDates(dataTable);
		shell.open();
	}

	private void insertDates(Table dataTable2) {
		TableItem item=null;
		for(int i=1;i<100;i++){
			item=new TableItem(dataTable, SWT.NONE);
			item.setText(new String[]{i+"","學生"+i,(i%2==0?"男":"女"),i+10+""});
		}
		
	}
	public static void main(String[] args) {
		FillLayoutLoginDemo demo=new FillLayoutLoginDemo();
		demo.open();
	}
}
相關文章
相關標籤/搜索