SWT中的布局之-----FormLayout(表格式布局)

表格式(FormLayout类) 表格式布局管理器,通过创建组件各个边的距离来布局组件,和GridLayout一样强大.

  用GridLayout与FormLayout都可以实现相同的界面效果,但有时使用后者会更有效,而且不会像GridLayout因为容器大小变化而导致布局错位.

  使用marignWidth,marginHeight设置边距(这两个属性,来设置容器的左边距和上边距(单位:像素))

  使用FormData的构造函数(FormLayout也有自己的布局数据类,他的使用方法是new FormData()或new FormData(int width,int height))

  FormAttachment类的用法

  FormAttachment是在FormData下的,更进一步的布局数据类,它的用法主要体现在它不同的构造函数中.

FormLayout1.java

 import org.eclipse.swt.SWT;
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 FormLayout1 {
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(327, 253);
// ---------创建窗口中的其他界面组件-------------
FormLayout formLayout = new FormLayout();
formLayout.marginWidth = 100; // 左边距,单位:像素
formLayout.marginHeight = 50; // 上边距
shell.setLayout(formLayout);
new Button(shell, SWT.NONE).setText("button1");
// -----------------END------------------------
shell.layout();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}

SWT中的布局之-----FormLayout(表格式布局)

1.使用marginWidth,marginHeight设置边距

这两个属性用来设置容器的左边距和上边距(单位:像素).下面给出一个具体的实例:

 public class FormLayout1 {
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(327, 253);
shell.setText("SWT Application");
// ------------------新插入的界面核心代码------------------------
FormLayout formLayout = new FormLayout();
formLayout.marginWidth = 100;
formLayout.marginHeight = 50;
shell.setLayout(formLayout);
new Button(shell, SWT.NONE).setText("button1");
// ------------------END---------------------------------------------
shell.layout();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}

SWT中的布局之-----FormLayout(表格式布局)

2.使用FormData的构造函数

FromLayout也有自己的布局数据类FormData,它的使用方法是:new FormData()或者new FormData(int width,int height)

 public class FormData1 {
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(327, 253);
shell.setText("SWT Application");
// ------------------新插入的界面核心代码------------------------
shell.setLayout(new FormLayout());
// new FormData ()
Button button1 = new Button(shell, SWT.NONE);
button1.setText("button1");
FormData formData = new FormData();
button1.setLayoutData(formData);
// new FormData (int width, int height),单位:像素
Button button2 = new Button(shell, SWT.NONE);
button2.setText("button2");
FormData formData2 = new FormData(200, 50);// button2变成200长,50宽
button2.setLayoutData(formData2);
// ------------------END---------------------------------------------
shell.layout();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}

SWT中的布局之-----FormLayout(表格式布局)

3.FormAttachment类的用法:

FromAttachment是在FormData下的,更进一步的布局数据类.它的用法体现在他不同的构造函数中.

(1) new FormAttachment(int numerator,int offset)

  button1的顶边(fromData.top)离shell容器的空白边距是shell容器总体空白长度的60%.

  偏移的点数(points)为0,效果如下:

         //>>>>>>>>>>>>>>>华丽丽的分割线>>>>>>>>>>>>>>>>>>>>>>>>>>>>
shell.setLayout(new FormLayout());
new Text(shell, SWT.BORDER).setText("text1");
//将button1应用FormData
Button button1 = new Button(shell, SWT.NONE);
button1.setText("button1"); FormData formData = new FormData();
formData.top = new FormAttachment(60, 0); // button1的顶端应用FormAttachment设置
button1.setLayoutData(formData); //>>>>>>>>>>>>>>>华丽丽的分割线>>>>>>>>>>>>>>>>>>>>>>>>>>>>

SWT中的布局之-----FormLayout(表格式布局)

如果改成了 formData.top = new FormAttachment(60, 30)

SWT中的布局之-----FormLayout(表格式布局)

从图中更以看出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为对齐方式.

  下面给出一个例子:

         //======================华丽丽的分割线===========================
shell.setLayout(new FormLayout());
Text text1 = new Text(shell, SWT.BORDER);
text1.setLayoutData(new FormData(100, 50));
//定义并设置FormData
FormData formData = new FormData();
//以text1为基准偏移50像素
FormAttachment formAttachment = new FormAttachment(text1,50);
formData.top = formAttachment;
formData.left = formAttachment;
// 将button1应用FormData
Button button1 = new Button(shell, SWT.NONE);
button1.setText("button1");
button1.setLayoutData(formData);
//======================华丽丽的分割线===========================

SWT中的布局之-----FormLayout(表格式布局)

new FormAttachment(text1,50,int alignment)中alignment的设置的效果如图所示,

表中的效果的程序就是按照上面的代码为基础修改"FormAttachment formAttachment = new FormAttachment(text1,50);"

这一句得到的.

SWT中的布局之-----FormLayout(表格式布局)

FormAttachment1.java

 public class FormAttachment1 {
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(327, 253);
// ---------创建窗口中的其他界面组件-------------
shell.setLayout(new FormLayout());
Button button = new Button(shell, SWT.NONE);
button.setText("button");
FormData formData = new FormData();
// 按钮的顶端(formData.top)离shell容器的空白边距是shell容器总体空白长度的60%
formData.top = new FormAttachment(60, 0);
button.setLayoutData(formData);
// -----------------END------------------------
shell.layout();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}

SWT中的布局之-----FormLayout(表格式布局)

FormAttachment2.java

 public class FormAttachment2 {
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(327, 253);
// ---------创建窗口中的其他界面组件-------------
shell.setLayout(new FormLayout());
Text text1 = new Text(shell, SWT.BORDER);
text1.setLayoutData(new FormData(100, 50));
// 定义并设置FormData
FormData formData = new FormData();
// 以text1为基准偏移50像素
FormAttachment formAttachment = new FormAttachment(text1, 50);
// FormAttachment formAttachment = new FormAttachment(text1, 50,
// SWT.LEFT);参数3的可选值包括SWT.DEFAULT、SWT.LEFT、SWT.CENTER、SWT.RIGHT、SWT.TOP、SWT.BOTTOM
formData.top = formAttachment;
formData.left = formAttachment;
// 将button1应用FormData
Button button1 = new Button(shell, SWT.NONE);
button1.setText("button1");
button1.setLayoutData(formData);
// -----------------END------------------------
shell.layout();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}

SWT中的布局之-----FormLayout(表格式布局)

上一篇:iConvert Icons 图标转换生成利器,支持Windows, Mac OS X, Linux, iOS,和Android等系统


下一篇:VueJs笔记