SWT之FormData布局.docx

上传人:b****7 文档编号:9624521 上传时间:2023-02-05 格式:DOCX 页数:11 大小:29.91KB
下载 相关 举报
SWT之FormData布局.docx_第1页
第1页 / 共11页
SWT之FormData布局.docx_第2页
第2页 / 共11页
SWT之FormData布局.docx_第3页
第3页 / 共11页
SWT之FormData布局.docx_第4页
第4页 / 共11页
SWT之FormData布局.docx_第5页
第5页 / 共11页
点击查看更多>>
下载资源
资源描述

SWT之FormData布局.docx

《SWT之FormData布局.docx》由会员分享,可在线阅读,更多相关《SWT之FormData布局.docx(11页珍藏版)》请在冰豆网上搜索。

SWT之FormData布局.docx

SWT之FormData布局

SWT之FormAttachment

转自:

    像其它的layout类一样,FormLayout用的data类是:

FormData。

FormData用另外一个类来控制窗口小部件的大小和位置:

FormAttachment.一个FormData最多用4个FormAttachment,它们分别对应这个小部件的4个面:

顶部,底部,左边和右边。

FormAttachment定义了小部件在parentcomposite或是这个composite里其它部件的位置。

FormAttachment计算位置和大小的方法:

y=ax+b

 

在这个等式中,在数学上y代表的是纵坐标,x是横坐标值.a是斜率,b是偏移量.按照FormAttachment的规则,y是高,x是宽度,a是一个相对其它部件的百分率,b是偏移量.FormAttachment实例中的每个数据成员分别代表这些值.

以下(表一)是FormAttachment的数据成员表和相应的意思:

表一:

FormAttachment数据成员

Attribute

Description

intalignment

指定是以另外一个部件的哪一边为基准的.可能的值是:

SWT.TOP,SWT.CENTER,和SWT.BOTTOM.默认是以最近的一边为基准.

Controlcontrol

指定FormAttachment是以哪个部件为参照物.

intdenominator

指定分母.默认值为100

intnumerator

指定分子

intoffset

指定离composite边界或部件的边界的偏移量.单位是象素.

FormAttachment拥有5个构造函数,没有一个是空的,它们如下表(表二)所示:

表二:

FormAttachment的构造函数:

Constructor

Description

FormAttachment(Controlcontrol)

以另外的某个部件为参照物.

FormAttachment(Controlcontrol,intoffset)

以另外的某个部件为参照物,加上偏移量.

FormAttachment(Controlcontrol,intoffset,intalignment)

以另外的某个部件为参照物,加上偏移量和alignment

FormAttachment(intnumerator)

设定分子.分母为100,没有偏移量

FormAttachment(intnumerator,intoffset)

指定分子和偏移量和100的分母

FormAttachment(intnumerator,intdenominator,intoffset)

特定的分子,分母,偏移量

FormData最多包含4个FormAttachment实例,每个对应了与它联系的部件的一边.另外,FormData也可以指定宽和高.表四列出了FormData的数据成员:

Attribute

Description

FormAttachmentbottom

TheFormAttachmentcorrespondingtothebottomsideofthecontrol.

这个FormAttachment用来指定部件的底部位置

intheight

这个部件的高度.单位为象素.

FormAttachmentleft

这个FormAttachment用来指定部件的左部位置

FormAttachmentright

这个FormAttachment用来指定部件的右部位置

FormAttachmenttop

这个FormAttachment用来指定部件的顶部位置

intwidth

这个部件的宽度.单位为象素.

当你生成一个FormData对象,你可以自己传给它宽和高的值.如果你没有指定FormAttachment对象,部件会自动以parentcomposite的上边界和左边为起始边界.如果你这样定义了多个部件,它们会都在composite的左上角.

FormLayout有两个数据成员marginHeight和marginWidth,用来以象素为单位来指定大小.它们用来指定包围composite里所有内容的空白.marginHeight对应的是顶部和底部的空白大小,marginWidth对应的是左边和右边的空白大小.空白的默认值是0.

以上的话如果你看得不大懂也没有关系,我们按照具体的例子来看就知道了.

用到FormLayout的最简单的例子就是一个窗口里一个按钮,并且不用FormData.

importorg.eclipse.swt.SWT;

importorg.eclipse.swt.layout.FormLayout;

importorg.eclipse.swt.widgets.Button;

importorg.eclipse.swt.widgets.Display;

importorg.eclipse.swt.widgets.Shell;

publicclassTestFormLayout{

publicstaticvoidmain(String[]args){

  Displaydisplay=newDisplay();

  Shellshell=newShell(display);

  shell.setLayout(newFormLayout());

  newButton(shell,SWT.PUSH).setText("Button");

  shell.pack();

  shell.open();

  while(!

shell.isDisposed()){

   if(!

display.readAndDispatch()){

    display.sleep();

   }

  }

  display.dispose();

}

}

通过上述代码我们可以简单的在一个shell上设置一个button,并且button距离整个界面的其他的边界距离为0。

我们可以通过设定marginHeight和marginWidth值改变周围的空白,即距离边界的距离.

代码象这样:

FormLayoutlayout=newFormLayout();

layout.marginHeight=5;

layout.marginWidth=10;

shell.setLayout(layout);

但是,只有用FormData和FormAttachment你才能做更多的事.

试着使用FormData,但是不用FormAttachments,FormData有公共成员属性height和width用来改变按钮的大小.

Buttonbutton=newButton(shell,SWT.PUSH);

button.setText("Button");

FormDatadata=newFormData();

data.height=50;

data.width=50;

button.setLayoutData(data);

按钮在窗口中的位置是静态的,也就是说改变窗口的大小不影响按钮的位置.

我们举个例子,比如说,你希望这个按钮始终离窗口的右边界50个象素的话,你必须在你刚刚产生的FormData上添加一个FormAttachment.

因为你现在要指定这个按钮的右边的具体情况了.所以你现在要设置FormData的right属性,

还记得FormData的right属性是一个FormAttachment对象吧.

当与parentcomposite产生联系时,如果是顶部和左边的话,分子设置为0,如果是顶部和右边的话,分子设置为100,分别代表了0%和100%.可以在构造函数中设置分子.我们现在把分子设置为100,偏移量设置为-50.

我们不需要设置分母,因为它的默认大小是100.代码象这样:

data.right=newFormAttachment(100,-50);

现在编译并运行程序,按钮离右边刚好50个象素.缩小窗口也不会有任何影响,按钮离右边始终是50象素.

现在按钮总是维持它离右边50个象素,

但是我们需要它始终保持位置不变,并且随着窗口的伸缩而伸缩.要实现这个要求,我们必须设置它的左边,

所以可以这样实现:

data.left=newFormAttachment(0,10);

把分子的值设置为0表示它是以窗口的最左边来计算偏移量的.

偏移量设置为10表示它始终离窗口最左边10个象素.

重新编译运行程序,初始界面好象和刚刚差不多,

但是改变窗口大小就可以看到按钮离窗口左边的大小始终没有改变.

离左边和离右边的值已经设置好了,所以它的宽度自然也定下来了,我们可以把原来FormData设置的按钮宽度那行代码删掉了.

现在有点熟悉了吧,现在我们把按钮联系到窗口的顶部和底部.我们可以把它放到离窗口顶部25%的距离.我们可以简单的表示为1/4,而不是0.25让分母保持为默认值(这样也可以).我们把偏移量设置为0.加上这句代码:

data.top=newFormAttachment(1,4,0);

现在按钮保持与顶部1/4的距离了.无论怎么改变窗口大小都是1/4。

让我们在这个按钮下面再添加一个按钮.我们把这个按钮布置在现在的按钮下面,添加按钮的顶部在现在的按钮的底部下面5个象素.并且左边和右边分别和现在的按钮对齐.从下面的代码开始:

Buttonbutton2=newButton(shell,SWT.PUSH);

button2.setText("Button2");

然后我们产生一个FormData对象,设置它的bottom成员.我们把分子设置为0,偏移量为0。

data=newFormData();

button2.setLayoutData(data);

data.bottom=newFormAttachment(100,0);

为了以现在的按钮为标准来算偏移量,我们只需要把第一个按钮传个FormAttachment的构造函数就可以了,那么现在就不是以窗口边界来计算,而是以传递给它的按钮来计算了.

我们要把这个按钮加到以有按钮的下面5个象素处,所以偏移量是5.

data.top=newFormAttachment(button,5);

我们不需要设置为以按钮的底部为界,因为默认情况下,边界被自动设是离这个按钮最近的边界.

因为我们指定的FormAttachment是用来指定新按钮的顶部的,而且新按钮是在第一个按钮后面加上去的,新按钮离第一个按钮的地方最近的就是底部.

你可能就用同样的构造函数来设置新按钮的左边界,甚至就用只需要一个Control对象的构造函数,因为偏移量是0.象这样:

data.left=newFormAttachment(button);

但是这句代码是以第一个按钮的右边为基准来计算的,如上图所示:

因为新添加的按钮左边离第一个按钮最近的边界是它的右边.

所以我们必须清楚地指定按第一个按钮的左边界为基准.

只需要传给构造函数SWT.LEFT值给FromData对象的alignment成员:

data.left=newFormAttachment(button,0,SWT.LEFT);

我们同样的方法设置新按钮的右边,完成任务.

data.right=newFormAttachment(button,0,SWT.RIGHT);

完整的代码如下:

importorg.eclipse.swt.widgets.*;

importorg.eclipse.swt.layout.FormAttachment;

importorg.eclipse.swt.layout.FormData;

importorg.eclipse.swt.layout.FormLayout;

importorg.eclipse.swt.SWT;

publicclassFormLayoutFormAttachment{

publicstaticvoidmain(String[]args){

   Displaydisplay=newDisplay();

   Shellshell=newShell(display);

   FormLayoutlayout=newFormLayout();

   layout.marginHeight=5;

   layout.marginWidth=10;

   shell.setLayout(layout);

   Buttonbutton=newButton(shell,SWT.PUSH);

   button.setText("Button");

   FormDatadata=newFormData();

   data.height=50;

   data.right=newFormAttachment(100,-50);

   data.left=newFormAttachment(0,10);

   data.top=newFormAttachment(1,4,0);

   button.setLayoutData(data);

   Buttonbutton2=newButton(shell,SWT.PUSH);

   button2.setText("Button2");

   data=newFormData();

   button2.setLayoutData(data);

   data.bottom=newFormAttachment(100,0);

   data.top=newFormAttachment(button,5);

   data.left=newFormAttachment(button,0,SWT.LEFT);

   data.right=newFormAttachment(button,0,SWT.RIGHT);

   shell.pack();

   shell.open();

   while(!

shell.isDisposed()){

     if(!

display.readAndDispatch()){

       display.sleep();

     }

   }

   display.dispose();

}

}

下面写一个更加复杂的程序,代码如下:

我就不多解释了,应该都看得懂,运行结果出来后,

无论怎么改变窗口的大小,所有按钮的位置都没有改变.

importorg.eclipse.swt.SWT;

importorg.eclipse.swt.layout.FormAttachment;

importorg.eclipse.swt.layout.FormData;

importorg.eclipse.swt.layout.FormLayout;

importorg.eclipse.swt.widgets.Button;

importorg.eclipse.swt.widgets.Display;

importorg.eclipse.swt.widgets.Shell;

publicclassTestFormLayout{

publicstaticvoidmain(String[]args){

  Displaydisplay=newDisplay();

  Shellshell=newShell(display);

  FormLayoutlayout=newFormLayout();

  layout.marginHeight=5;

  layout.marginWidth=10;

  shell.setLayout(layout);

  Buttonbutton=newButton(shell,SWT.PUSH);

  button.setText("Button");

  FormDatadata=newFormData();

  data.height=50;

  data.right=newFormAttachment(100,-50);

  data.left=newFormAttachment(0,10);

  data.top=newFormAttachment(1,4,0);

  button.setLayoutData(data);

  Buttonbutton2=newButton(shell,SWT.PUSH);

  button2.setText("Button2");

  data=newFormData();

  button2.setLayoutData(data);

  data.bottom=newFormAttachment(100,0);

  data.top=newFormAttachment(button,5);

  data.left=newFormAttachment(button,0,SWT.LEFT);

  data.right=newFormAttachment(button,0,SWT.RIGHT);

  shell.pack();

  shell.open();

  while(!

shell.isDisposed()){

   if(!

display.readAndDispatch()){

    display.sleep();

   }

  }

  display.dispose();

}

}

下面写一个更加复杂的程序,代码如下:

运行结果出来后,无论怎么改变窗口的大小,所有按钮的位置都没有改变.

importorg.eclipse.swt.SWT;

importorg.eclipse.swt.layout.FormAttachment;

importorg.eclipse.swt.layout.FormData;

importorg.eclipse.swt.layout.FormLayout;

importorg.eclipse.swt.layout.GridData;

importorg.eclipse.swt.layout.GridLayout;

importorg.eclipse.swt.widgets.Button;

importorg.eclipse.swt.widgets.Composite;

importorg.eclipse.swt.widgets.Display;

importorg.eclipse.swt.widgets.Shell;

publicclassFormLayoutComplex{

publicstaticvoidmain(String[]args){

  Displaydisplay=newDisplay();

  Shellshell=newShell(display);

  FormLayoutlayout=newFormLayout();

  shell.setLayout(layout);

  Buttonone=newButton(shell,SWT.PUSH);

  one.setText("One");

  FormDatadata=newFormData();

  data.top=newFormAttachment(0,5);

  data.left=newFormAttachment(0,5);

  data.bottom=newFormAttachment(50,-5);

  data.right=newFormAttachment(50,-5);

  one.setLayoutData(data);

  Compositecomposite=newComposite(shell,SWT.NONE);

  GridLayoutgridLayout=newGridLayout();

  gridLayout.marginHeight=0;

  gridLayout.marginWidth=0;

  composite.setLayout(gridLayout);

  Buttontwo=newButton(composite,SWT.PUSH);

  two.setText("two");

  GridDatagridData=newGridData(GridData.FILL_BOTH);

  two.setLayoutData(gridData);

  Buttonthree=newButton(composite,SWT.PUSH);

  three.setText("three");

  gridData=newGridData(GridData.FILL_BOTH);

  three.setLayoutData(gridData);

  Buttonfour=newButton(composite,SWT.PUSH);

  four.setText("four");

  gridData=newGridData(GridData.FILL_BOTH);

  four.setLayoutData(gridData);

  data=newFormData();

  data.top=newFormAttachment(0,5);

  data.left=newFormAttachment(one,5);

  data.bottom=newFormAttachment(50,-5);

  data.right=newFormAttachment(100,-5);

  composite.setLayoutData(data);

  Buttonfive=newButton(shell,SWT.PUSH);

  five.setText("five");

  data=newFormData();

  data.top=newFormAttachment(one,5)

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 党团工作 > 入党转正申请

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1