表格式(FormLayout類) 表格式布局管理器,通過創建組件各個邊的距離來布局組件,和GridLayout一樣強大.
用GridLayout與FormLayout都可以實現相同的界面效果,但有時使用后者會更有效,而且不會像GridLayout因為容器大小變化而導致布局錯位.
使用marignWidth,marginHeight設置邊距(這兩個屬性,來設置容器的左邊距和上邊距(單位:像素))
使用FormData的構造函數(FormLayout也有自己的布局數據類,他的使用方法是new FormData()或new FormData(int width,int height))
FormAttachment類的用法
FormAttachment是在FormData下的,更進一步的布局數據類,它的用法主要體現在它不同的構造函數中.
FormLayout1.java
1 import org.eclipse.swt.SWT; 2 import org.eclipse.swt.layout.FormLayout; 3 import org.eclipse.swt.widgets.Button; 4 import org.eclipse.swt.widgets.Display; 5 import org.eclipse.swt.widgets.Shell; 6 7 public class FormLayout1 { 8 public static void main(String[] args) { 9 final Display display = Display.getDefault(); 10 final Shell shell = new Shell(); 11 shell.setSize(327, 253); 12 // ---------創建窗口中的其他界面組件------------- 13 FormLayout formLayout = new FormLayout(); 14 formLayout.marginWidth = 100; // 左邊距,單位:像素 15 formLayout.marginHeight = 50; // 上邊距 16 shell.setLayout(formLayout); 17 new Button(shell, SWT.NONE).setText("button1"); 18 // -----------------END------------------------ 19 shell.layout(); 20 shell.open(); 21 while (!shell.isDisposed()) { 22 if (!display.readAndDispatch()) 23 display.sleep(); 24 } 25 display.dispose(); 26 } 27 }
1.使用marginWidth,marginHeight設置邊距
這兩個屬性用來設置容器的左邊距和上邊距(單位:像素).下面給出一個具體的實例:
1 public class FormLayout1 { 2 public static void main(String[] args) { 3 final Display display = Display.getDefault(); 4 final Shell shell = new Shell(); 5 shell.setSize(327, 253); 6 shell.setText("SWT Application"); 7 // ------------------新插入的界面核心代碼------------------------ 8 FormLayout formLayout = new FormLayout(); 9 formLayout.marginWidth = 100; 10 formLayout.marginHeight = 50; 11 shell.setLayout(formLayout); 12 new Button(shell, SWT.NONE).setText("button1"); 13 // ------------------END--------------------------------------------- 14 shell.layout(); 15 shell.open(); 16 while (!shell.isDisposed()) { 17 if (!display.readAndDispatch()) { 18 display.sleep(); 19 } 20 } 21 } 22 }
2.使用FormData的構造函數
FromLayout也有自己的布局數據類FormData,它的使用方法是:new FormData()或者new FormData(int width,int height)
1 public class FormData1 { 2 public static void main(String[] args) { 3 final Display display = Display.getDefault(); 4 final Shell shell = new Shell(); 5 shell.setSize(327, 253); 6 shell.setText("SWT Application"); 7 // ------------------新插入的界面核心代碼------------------------ 8 shell.setLayout(new FormLayout()); 9 // new FormData () 10 Button button1 = new Button(shell, SWT.NONE); 11 button1.setText("button1"); 12 FormData formData = new FormData(); 13 button1.setLayoutData(formData); 14 // new FormData (int width, int height),單位:像素 15 Button button2 = new Button(shell, SWT.NONE); 16 button2.setText("button2"); 17 FormData formData2 = new FormData(200, 50);// button2變成200長,50寬 18 button2.setLayoutData(formData2); 19 // ------------------END--------------------------------------------- 20 shell.layout(); 21 shell.open(); 22 while (!shell.isDisposed()) { 23 if (!display.readAndDispatch()) { 24 display.sleep(); 25 } 26 } 27 } 28 }
3.FormAttachment類的用法:
FromAttachment是在FormData下的,更進一步的布局數據類.它的用法體現在他不同的構造函數中.
(1) new FormAttachment(int numerator,int offset)
button1的頂邊(fromData.top)離shell容器的空白邊距是shell容器總體空白長度的60%.
偏移的點數(points)為0,效果如下:
1 //>>>>>>>>>>>>>>>華麗麗的分割線>>>>>>>>>>>>>>>>>>>>>>>>>>>> 2 shell.setLayout(new FormLayout()); 3 new Text(shell, SWT.BORDER).setText("text1"); 4 //將button1應用FormData 5 Button button1 = new Button(shell, SWT.NONE); 6 button1.setText("button1"); 7 8 FormData formData = new FormData(); 9 formData.top = new FormAttachment(60, 0); // button1的頂端應用FormAttachment設置 10 button1.setLayoutData(formData); 11 12 //>>>>>>>>>>>>>>>華麗麗的分割線>>>>>>>>>>>>>>>>>>>>>>>>>>>>
如果改成了 formData.top = new FormAttachment(60, 30)
從圖中更以看出FormAttachment(60,30)是先按照FormAttachment(60,0)
的方式布局后,再下移動10個像素.這個地方有一個布局的次序.
new FormAttachment(int numerator)相當於new FormAttachment(int numerator,int offset)
當offset=0時,new FormAttachment(int numerator,int offset)相當於FormAttachmetn(int numerator,int denominator,int offset)當denominator(分母的意思)=100時,其中denominator是分母,例如FormAttachment(30,50,0)就是長度比例為30/50=60%,也就是和FormAttachment(60,0)的效果是一樣的.
(2) new FormAttachment(Control control,int offset,int alignment)
參數1是一個Control類,一般在使用的時候,都傳入一個組件(如文本框來做參數),應用此FormAttachment的組件將
依據參數1的contorl為基准來布局,offset為離control偏移量(單位:像素),alignment為對齊方式.
下面給出一個例子:
1 //======================華麗麗的分割線=========================== 2 shell.setLayout(new FormLayout()); 3 Text text1 = new Text(shell, SWT.BORDER); 4 text1.setLayoutData(new FormData(100, 50)); 5 //定義並設置FormData 6 FormData formData = new FormData(); 7 //以text1為基准偏移50像素 8 FormAttachment formAttachment = new FormAttachment(text1,50); 9 formData.top = formAttachment; 10 formData.left = formAttachment; 11 // 將button1應用FormData 12 Button button1 = new Button(shell, SWT.NONE); 13 button1.setText("button1"); 14 button1.setLayoutData(formData); 15 //======================華麗麗的分割線===========================
new FormAttachment(text1,50,int alignment)中alignment的設置的效果如圖所示,
表中的效果的程序就是按照上面的代碼為基礎修改"FormAttachment formAttachment = new FormAttachment(text1,50);"
這一句得到的.
FormAttachment1.java
1 public class FormAttachment1 { 2 public static void main(String[] args) { 3 final Display display = Display.getDefault(); 4 final Shell shell = new Shell(); 5 shell.setSize(327, 253); 6 // ---------創建窗口中的其他界面組件------------- 7 shell.setLayout(new FormLayout()); 8 Button button = new Button(shell, SWT.NONE); 9 button.setText("button"); 10 FormData formData = new FormData(); 11 // 按鈕的頂端(formData.top)離shell容器的空白邊距是shell容器總體空白長度的60% 12 formData.top = new FormAttachment(60, 0); 13 button.setLayoutData(formData); 14 // -----------------END------------------------ 15 shell.layout(); 16 shell.open(); 17 while (!shell.isDisposed()) { 18 if (!display.readAndDispatch()) 19 display.sleep(); 20 } 21 display.dispose(); 22 } 23 }
FormAttachment2.java
1 public class FormAttachment2 { 2 public static void main(String[] args) { 3 final Display display = Display.getDefault(); 4 final Shell shell = new Shell(); 5 shell.setSize(327, 253); 6 // ---------創建窗口中的其他界面組件------------- 7 shell.setLayout(new FormLayout()); 8 Text text1 = new Text(shell, SWT.BORDER); 9 text1.setLayoutData(new FormData(100, 50)); 10 // 定義並設置FormData 11 FormData formData = new FormData(); 12 // 以text1為基准偏移50像素 13 FormAttachment formAttachment = new FormAttachment(text1, 50); 14 // FormAttachment formAttachment = new FormAttachment(text1, 50, 15 // SWT.LEFT);參數3的可選值包括SWT.DEFAULT、SWT.LEFT、SWT.CENTER、SWT.RIGHT、SWT.TOP、SWT.BOTTOM 16 formData.top = formAttachment; 17 formData.left = formAttachment; 18 // 將button1應用FormData 19 Button button1 = new Button(shell, SWT.NONE); 20 button1.setText("button1"); 21 button1.setLayoutData(formData); 22 // -----------------END------------------------ 23 shell.layout(); 24 shell.open(); 25 while (!shell.isDisposed()) { 26 if (!display.readAndDispatch()) 27 display.sleep(); 28 } 29 display.dispose(); 30 } 31 }