组织SWT/JFace控件的利器:Layout

王朝other·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

在可视化编程时代,大多数可视化的GUI开发工具都提供了按一定规则排列Form中的控件的功能。但是对于java来说,支持可视化开发的工具并不多,虽然有一些这样的工具,但它们大多是第三方的产品,稳定性方面有一些欠缺。因此,在很多时候使用Java编写GUI程序时,就要使用布局(Layout)来控制Form上的控件的位置。

本文主要讨论如何使用SWT中提供的布局来安排控件的位置,并通过实例来演示这一过程。在SWT中提供了5种布局:FillLayout, RowLayout, GridLayout, FormLayout, and StackLayout。下面我将具体讨论这5种布局的使用。

FillLayout

FillLayout是最简单的布局。它可以将控件横向或纵向进行排列,并且其中每个控件都有同样的宽度或高度。使用FillLayout一般分为2步。

1. 建立一个FillLayout对象。

2. 使用setLayout方法设置Shell对象的布局。

下面代码使用FillLayout在Shell上放了3个按钮,代码如下:

package layout;

import org.eclipse.swt.SWT;

import org.eclipse.swt.widgets.*;

import org.eclipse.swt.layout.*;

public class TestFillLayout

{

public static void main(String[] args)

{

Display display = new Display();

Shell shell = new Shell(display, SWT.DIALOG_TRIM);

shell.setText("FillLayout演示");

shell.setSize(400, 300);

// 设置shell的布局

FillLayout layout = new FillLayout();

shell.setLayout(layout);

// 向shell添加控件

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

button1.setText("按钮1");

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

button2.setText("按钮2");

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

button3.setText("按钮3");

shell.open();

while (!shell.isDisposed())

{

if (!display.readAndDispatch())

{

display.sleep();

}

}

display.dispose();

}

}

界面如图1所示。

图 1 使用横向FillLayout的Shell界面

假如想要Shell上的控件纵向排列,可以在建立布局时将type属性设置成SWT.VERTICAL。代码如下:

FillLayout layout = new FillLayout();

layout.type = SWT.VERTICAL;

shell.setLayout(layout);

图2是控件纵向排列的效果图

图 2 使用纵向FillLayout的Shell界面

FillLayout的构造函数重载了2次。其中一个构造函数有一个参数,这个参数就是type。因此,我们也可以通过FillLayout的构造函数对type赋值。

shell.setLayout(new FillLayout(SWT.VERTICAL));

RowLayout

RowLayout的功能和FillLayout差不多。只是它和FillLayout的最大区别是每个控件并不一定是一样大小。而且RowLayout是按行排列,这一点和FillLayout是不同的。在一行排满后,就从下一行开始排列。和RowLayout配合使用的还有一个RowData类。这个类可以设置每一个控件的大小。下面代码是一个使用RowLayout的小例子。

package layout;

import org.eclipse.swt.SWT;

import org.eclipse.swt.widgets.*;

import org.eclipse.swt.layout.*;

public class TestRowLayout

{

public static void main(String[] args)

{

Display display = new Display();

Shell shell = new Shell(display, SWT.DIALOG_TRIM);

shell.setText("RowLayout演示");

shell.setSize(220, 200);

// 将Shell的布局设置成RowLayout

RowLayout layout = new RowLayout(SWT.HORIZONTAL);

layout.spacing = 30;

layout.marginLeft = 30;

layout.marginTop = 30;

shell.setLayout(layout);

RowData rowData = new RowData();

rowData.height = 50;

rowData.width = 100;

// 向shell添加控件

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

button1.setText("按钮1");

button1.setLayoutData(rowData);

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

button2.setText("按钮2");

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

button3.setText("按钮3");

shell.open();

while (!shell.isDisposed())

{

if (!display.readAndDispatch())

{

display.sleep();

}

}

display.dispose();

}

}

图3是使用RowLayout的效果图

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航