SWT的FormLayout

SWT体系下的FormLayout是非常灵活、精确的布局,FormLayout布局组件的特点是采用百分比+偏移量的方式。前者可以应付容器尺寸变化时内部组件随之等比例调整;后者以应付精确的布局。这一特征是通过org.eclipse.swt.layout.FormData和org.eclipse.swt.layout.FormAttachment两个类来实现。
通常使用FormLayout来定位一个组件要确定4个FormAttachment对象:top、bottom、left、right,即组件的4条边。而且通常是使用FormAttachment(int numerator,int offset)这个构造器,也就是百分比+偏移量。当然FormAttachment不只这一种,但是都是可选的,如果想深入研究FormLayout可以参阅SWT相关的介绍。
下面给出一段SWT示例程序:

         public static void main(String[] args) {

                   Display display = new Display();

                   Shell shell = new Shell(display);

                   shell.setText("SWT Application");

                   shell.setLayout(new FormLayout());

                   final Button button = new Button(shell, SWT.NONE);

                   button.setText("button");

                   final FormData formData = new FormData();

                   formData.top = new FormAttachment(20, 0);

                   formData.left = new FormAttachment(50, 0);

                   formData.bottom = new FormAttachment(20, 30);

                   formData.right = new FormAttachment(50, 50);

                   button.setLayoutData(formData);

                   shell.open();

                   while (!shell.isDisposed()) {

                            if (!display.readAndDispatch()) {

                                     display.sleep();

                            }

                   }

                   display.dispose();

         }

FormLayout通过指定组件的四条边来完成布局。

posted @ 2012-10-18 14:01  rhino  阅读(2580)  评论(0编辑  收藏  举报