SWT界面佈局介紹之AbsoluteLayout

AbsoluteLayout是SWT界面設計的默認佈局方式,採用座標絕對定位的方式,定義組件的位置和大小。 java

默認不設置父控件的Layout或者顯示使用setLayout(null)設置父控件的佈局爲AbsoluteLayout絕對佈局 shell

例子:使用絕對定位佈局製做系統登陸界面 eclipse

要點:設置窗口只有關閉按鈕 防止窗口resize後登陸界面變形 難看 佈局

截圖1、主窗體效果spa

login

截圖2、窗體組件大綱視圖.net

login_outline

java代碼:設計

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * SWT AbsoluteLayout佈局使用demo 系統登陸界面設計
 * @author xwalker
 *
 */
public class AbsoluteLayoutLoginDemo{
	private Text unameText;
	private Text pwdText;
	private Shell shell;
	
	public void open() {
		Display display = Display.getDefault();
		createContents();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}
	/**
	 * 建立窗口組件
	 */
	protected void createContents(){
		shell=new Shell(SWT.CLOSE);
		shell.setSize(300, 227);
		shell.setLayout(null);
		shell.setText("SWT AbsoluteLayout佈局使用");
		unameText = new Text(shell, SWT.BORDER);
		unameText.setBounds(101, 27, 152, 23);
		pwdText = new Text(shell, SWT.BORDER);
		pwdText.setBounds(101, 59, 152, 23);
		
		Label unameLabel = new Label(shell, SWT.NONE);
		unameLabel.setBounds(22, 30, 61, 17);
		unameLabel.setText("用戶名");
		
		Label pwdlabel = new Label(shell, SWT.NONE);
		pwdlabel.setBounds(22, 62, 61, 17);
		pwdlabel.setText("密碼");
		
		Button remeberCheckbox = new Button(shell, SWT.CHECK);
		remeberCheckbox.setBounds(101, 102, 98, 17);
		remeberCheckbox.setText("記住登陸帳戶");
		
		Button loginBtn = new Button(shell, SWT.NONE);
		loginBtn.setBounds(34, 134, 80, 27);
		loginBtn.setText("登陸系統");
		
		Button cancelBtn = new Button(shell, SWT.NONE);
		cancelBtn.setBounds(147, 134, 80, 27);
		cancelBtn.setText("取消登陸");
		shell.open();
	}

	public static void main(String[] args) {
		AbsoluteLayoutLoginDemo demo=new AbsoluteLayoutLoginDemo();
		demo.open();
	}
}

注意:通常不推薦使用AbsoluteLayout 佈局參數調整麻煩,窗口resize後佈局不能適應。code

相關文章
相關標籤/搜索