The test code is as follows:
Copy Code code 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 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 ();
}
}
Look at one more example:
Copy Code code 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 shell = new shell (display);
Label label = new label (Shell, SWT. WRAP);
Label
. SetText ("This is a long-text string that would 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 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 (-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 ();
}
}