RowLayout

RowLayout

   因为可以换行布局及可以设置间距和填充,所以RowLayout 使用比FillLayou 广泛的多。 RowLayout 有几个配置字段。甚至其中每个部件的宽度和高度都可以通过部件 的RowData ,使用setLayoutData 方法精确 的设定。

RowLayout 的配置字段

Type       *New in 2.0*

    Type 字段用来设置RowLayout 放置部件是按水平方向放置或垂直方向放置。默认值为水平方向放置 SWT.HORIZONTAL

       Warp

    Warp 字段是用来设置当此行没有足够的空间来放置部件时RowLayout 是否要将部件换到下一行上来显示,默认值为true

    Pack

    当Pack 字段为真时RowLayout 中的部件将会保持他们原来的大小,并且是左对齐的。如果Pack 的值是false 的则部件将填满整个可利用的区域,跟FillLayout 相似 。其默认值是true

    Justify

    如果Justify 为true 时RowLayout 中的 各个部件将会在整个可利用区域从左到右依次展开排列,如果起父容器Composite 的变宽时,则其空白区域将会平均的分配大到各个部件。如果Justify 和Pack 同时为true ,则各部件将使用他们原来的大小,而空白区域被放置到各 个部件中间,以便可以正当的充满其区域。Justify 的默认值为false

    MarginLeft, MarginTop, MarginRight, MarginBottom and Spacing

    这写字段用像素来控制部件与部件之间(间距,spacing )、部件和其容器之间(填充,margin )宽度。默认的 RowLayout 的填充和间距为3 像素宽。各个 字段如下所示:

RowLayout 的示例

    下面代码首先创建RowLayout 对象然后 设置其各个字段均不为默认属性,然后将其设置为shell 的布局。

      RowLayout rowLayout = new RowLayout();

      rowLayout.wrap = false;

      rowLayout.pack = false;

      rowLayout.justify = true;

      rowLayout.type = SWT.VERTICAL;

      rowLayout.marginLeft = 5;

      rowLayout.marginTop = 5;

      rowLayout.marginRight = 5;

      rowLayout.marginBottom = 5;

      rowLayout.spacing = 0;

      shell.setLayout(rowLayout);

如果你要使用默认的设置则只需要一行代码:

shell.setLayout( new RowLayout());

下面的表格展示了其字段详细的设置及运行结果:

Initial

After resize

wrap = true

pack = true

justify = false

type = SWT.HORIZONTAL

(defaults)

wrap = false

(clips if not enough space)

pack = false

(all widgets are the same size)

justify = true

(widgets are spread across the available space)

type = SWT.VERTICAL

(widgets are arranged vertically in columns)

 

使用RowData 对象和RowLayout

每个被RowLayout 控制的部件可以通过设置RowData 来控制其宽度和高度。下面的代码是使用了RowData 来改变shell 中按钮的初始大小。

import org.eclipse.swt.*;

import org.eclipse.swt.widgets.*;

import org.eclipse.swt.layout.*;

public class RowDataExample {

   public static void main(String[] args) {

       Display display = new Display();

       Shell shell = new Shell(display);

       shell.setLayout( new RowLayout());

       Button button1 = new Button(shell, SWT.PUSH);

       button1.setText( "Button 1" );

       button1.setLayoutData( new RowData(50, 40));

       Button button2 = new Button(shell, SWT.PUSH);

       button2.setText( "Button 2" );

       button2.setLayoutData( new RowData(50, 30));

       Button button3 = new Button(shell, SWT.PUSH);

       button3.setText( "Button 3" );

       button3.setLayoutData( new RowData(50, 20));

       shell.pack();

       shell.open();

       while (!shell.isDisposed()) {

          if (!display.readAndDispatch()) display.sleep();

       }

   }

}

    运行这些代码后效果如图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值