FormLayout(表单布局)通过设定控件之间的偏移关系来进行布局。
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; import org.eclipse.swt.widgets.Text;public class FormLayoutExample {
public static void main(String[] args) {
FormData formData;
Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("FormLayout Example");
shell.setBounds(100, 100, 220, 180);
shell.setLayout(new FormLayout());
Button cancelButton = new Button(shell, SWT.PUSH);
cancelButton.setText("Cancel");
formData = new FormData();
formData.right = new FormAttachment(100, -5);
formData.bottom = new FormAttachment(100, -5);
cancelButton.setLayoutData(formData);
Button okButton = new Button(shell, SWT.PUSH);
okButton.setText("OK");
formData = new FormData();
formData.right = new FormAttachment(cancelButton, -5);
formData.bottom = new FormAttachment(100, -5);
okButton.setLayoutData(formData);
Text text = new Text(shell, SWT.MULTI | SWT.BORDER);
formData = new FormData();
formData.top = new FormAttachment(0, 5);
formData.bottom = new FormAttachment(cancelButton, -5);
formData.left = new FormAttachment(0, 5);
formData.right = new FormAttachment(100, -5);
text.setLayoutData(formData);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
结果如下:
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于