1. Setting margins using Marginwidth,marginheight
These two properties are used to set the container's left and top margins (in pixels). Here is a specific example:
1 Public classFORMLAYOUT1 {2 Public Static voidMain (string[] args) {3 FinalDisplay display =Display.getdefault ();4 FinalShell Shell =NewShell ();5Shell.setsize (327, 253);6Shell.settext ("SWT Application");7 //------------------The newly inserted interface core code------------------------ 8 formlayout formlayout = new formlayout (); 9 formlayout.marginwidth = +; Formlayout.marginheight = ten ; shell.setlayout (formlayout); A New Button (Shell, SWT. NONE). SetText ("button1"); - //------------------END--------------------------------------------- - shell.layout (); the Shell.open (); - while(!shell.isdisposed ()) { - if(!Display.readanddispatch ()) { - display.sleep (); + } - } + } A}
2. Using the Formdata constructor
Fromlayout also has its own layout data class FormData, which is used in the following ways: New FormData () or new FormData (int width,int height)
1 Public classFormData1 {2 Public Static voidMain (string[] args) {3 FinalDisplay display =Display.getdefault ();4 FinalShell Shell =NewShell ();5Shell.setsize (327, 253);6Shell.settext ("SWT Application");7 //------------------The newly inserted interface core code------------------------8Shell.setlayout (Newformlayout ());9 //new FormData ()TenButton button1 =NewButton (Shell, SWT. NONE); OneButton1.settext ("Button1"); AFormData FormData =NewFormData (); - Button1.setlayoutdata (formData); - //new FormData (int width, int height), units: pixels theButton button2 =NewButton (Shell, SWT. NONE); -Button2.settext ("Button2"); -FormData FormData2 =NewFormData (200, 50);//Button2 becomes 200 long, 50 wide - Button2.setlayoutdata (FORMDATA2); + //------------------END--------------------------------------------- - shell.layout (); + Shell.open (); A while(!shell.isdisposed ()) { at if(!Display.readanddispatch ()) { - display.sleep (); - } - } - } -}
Usage of the 3.FormAttachment class:
Fromattachment is a further layout data class under Formdata. Its usage is reflected in his different constructors.
(1) New formattachment (int numerator,int offset)
The white margin of the Button1 's top edge (fromdata.top) from the shell container is 60% of the total white space length of the shell container.
The offset point (points) is 0, and the effect is as follows:
1 //>>>>>>>>>>>>>>> Gorgeous split line >>>>>>>>> >>>>>>>>>>>>>>>>>>>2Shell.setlayout (Newformlayout ());3 NewText (Shell, SWT. BORDER). SetText ("Text1");4 //Apply button1 to Formdata5Button button1 =NewButton (Shell, SWT. NONE);6Button1.settext ("Button1");7 8FormData FormData =NewFormData ();9Formdata.top =NewFormattachment (60, 0);//button1 the top of the app Formattachment settingsTen Button1.setlayoutdata (formData); One A //>>>>>>>>>>>>>>> Gorgeous split line >>>>>>>>> >>>>>>>>>>>>>>>>>>>
If changed to Formdata.top = new Formattachment (60, 30)
More to see Formattachment (60,30) is the first according to Formattachment (60,0)
After the layout, move the 10 pixels down. This place has a layout order.
New formattachment (int numerator) equals new formattachment (int numerator,int offset)
When Offset=0, new formattachment (int numerator,int offset) is equivalent to FORMATTACHMETN (int numerator,int denominator,int offset) When denominator (denominator meaning) =100, where denominator is the denominator, for example formattachment (30,50,0) is the length ratio 30/50=60%, i.e. and formattachment (60,0) The effect is the same.
(2) New Formattachment (Control control,int offset,int Alignment)
Parameter 1 is a control class, which is usually used when a component (such as a text box) is passed in, and the component applying this formattachment will
The layout is based on the contorl of parameter 1, offset is the off-control offset (in pixels), and alignment is aligned.
An example is given below:
1 //====================== Gorgeous split-line ===========================2Shell.setlayout (Newformlayout ());3Text Text1 =NewText (Shell, SWT. BORDER);4Text1.setlayoutdata (NewFormData (100, 50));5 //define and set Formdata6FormData FormData =NewFormData (); 7// Text1-base offset 50 pixels 8 formattachment formattachment = new F Ormattachment (text1,50); 9Formdata.top =formattachment;TenFormdata.left =formattachment; One //Apply button1 to Formdata AButton button1 =NewButton (Shell, SWT. NONE); -Button1.settext ("Button1"); - Button1.setlayoutdata (formData); the //====================== Gorgeous split-line ===========================
The effect of setting alignment in New Formattachment (text1,50,int alignment),
The procedure for the effect in the table is to modify the "formattachment formattachment = new Formattachment (text1,50)" Based on the code above.
This sentence is obtained.
-----of the layout in SWT formlayout (tabular layout)