Test Code As follows:
Copy code The Code is as follows: Package swt_jface.demo2;
Import org. Eclipse. SWT. SWT;
Import org. Eclipse. SWT. layout. formattachment;
Import org. Eclipse. SWT. layout. formdata;
Import org. Eclipse. SWT. layout. formlayout;
Import org. Eclipse. SWT. Widgets. Button;
Import org. Eclipse. SWT. Widgets. display;
Import org. Eclipse. SWT. Widgets. shell;
Public class formlayoutsample {
Display display = New Display ();
Shell shell = new shell (Display );
Public formlayoutsample (){
Shell. setlayout (New formlayout ());
Button button1 = new button (shell, SWT. Push );
Button1.settext ("button1 ");
Formdata = new formdata ();
Formdata. Left = new formattachment (20 );
Formdata. Top = new formattachment (20 );
Button1.setlayoutdata (formdata );
Button button2 = new button (shell, SWT. Push );
Button2.settext ("button number 2 ");
Formdata = new formdata ();
Formdata. Left = new formattachment (button1, 0, SWT. center );
Formdata. Top = new formattachment (button1, 0, SWT. center );
Button2.setlayoutdata (formdata );
// Button button3 = new button (shell, SWT. Push );
// Button3.settext ("3 ");
//
// Formdata = new formdata ();
// Formdata. Top = new formattachment (button2, 10 );
// Formdata. Left = new formattachment (button2, 0, SWT. Left );
// Button3.setlayoutdata (formdata );
Shell. Pack ();
// Shell. setsize (500,600 );
Shell. open ();
While (! Shell. isdisposed ()){
If (! Display. readanddispatch ()){
Display. Sleep ();
}
}
Display. Dispose ();
}
Public static void main (string [] ARGs ){
New formlayoutsample ();
}
}
Let's look at another example: Copy code The Code is as follows: Package swt_jface.demo2;
Import org. Eclipse. SWT. SWT;
Import org. Eclipse. SWT. Graphics. Point;
Import org. Eclipse. SWT. Graphics. rectangle;
Import org. Eclipse. SWT. layout. formattachment;
Import org. Eclipse. SWT. layout. formdata;
Import org. Eclipse. SWT. layout. formlayout;
Import org. Eclipse. SWT. Widgets. Button;
Import org. Eclipse. SWT. Widgets. display;
Import org. Eclipse. SWT. Widgets. event;
Import org. Eclipse. SWT. Widgets. label;
Import org. Eclipse. SWT. Widgets. List;
Import org. Eclipse. SWT. Widgets. listener;
Import org. Eclipse. SWT. Widgets. shell;
Public class main {
Public static void main (string [] ARGs ){
Display display = New Display ();
Final shell = new shell (Display );
Label Label = new label (shell, SWT. Wrap );
Label
. Settext ("this is a long text string that will wrap when the dialog is resized .");
List list = new list (shell, SWT. Border | SWT. h_scroll | SWT. v_scroll );
List. setitems (New String [] {"item 1", "item2 "});
Button button1 = new button (shell, SWT. Push );
Button1.settext ("OK ");
Button button2 = new button (shell, SWT. Push );
Button2.settext ("cancel ");
Final int insetx = 4, insety = 4;
Formlayout = new formlayout ();
Formlayout. marginwidth = insetx;
Formlayout. marginheight = insety;
Shell. setlayout (formlayout );
Point size = label. computesize (SWT. Default, SWT. Default );
Final formdata labeldata = new formdata (size. X, SWT. Default );
Labeldata. Left = new formattachment (0, 0 );
Labeldata. Right = new formattachment (100, 0 );
Label. setlayoutdata (labeldata );
Shell. addlistener (SWT. Resize, new listener (){
Public void handleevent (event e ){
Rectangle rect = shell. getclientarea ();
Labeldata. width = rect. Width-insetx * 2;
Shell. layout ();
}
});
Formdata button2data = new formdata ();
Button2data. Right = new formattachment (100,-insetx );
Button2data. Bottom = new formattachment (100, 0 );
Button2.setlayoutdata (button2data );
Formdata button1data = new formdata ();
Button1data. Right = new formattachment (button2,-insetx );
Button1data. Bottom = new formattachment (100, 0 );
Button1.setlayoutdata (button1data );
Formdata listdata = new formdata ();
Listdata. Left = new formattachment (0, 0 );
Listdata. Right = new formattachment (100, 0 );
Listdata. Top = new formattachment (Label, insety );
Listdata. Bottom = new formattachment (button2,-insety );
List. setlayoutdata (listdata );
Shell. Pack ();
Shell. open ();
While (! Shell. isdisposed ()){
If (! Display. readanddispatch ())
Display. Sleep ();
}
Display. Dispose ();
}
}