Java程序设计之swt教程 下载本文

内容发布更新时间 : 2024/4/29 1:32:45星期一 下面是文章的全部内容请认真阅读。

package edu.ch4;

import org.eclipse.swt.SWT; import org.eclipse.swt.layout.*; import org.eclipse.swt.widgets.*; public class Sample4_12 { }

public static void main(String[] args) { }

Display display=new Display();//创建一个display对象。 final Shell shell=new Shell(display);//shell是程序的主窗体 shell.setText(\示例\);

RowLayout rowlayout=new RowLayout(); //创建按行放置组件的对象 rowlayout.pack=false; //强制组件大小相同 rowlayout.wrap=false; //不自动折行

rowlayout.marginWidth=20; //组件距容器边缘的宽度为20像素 rowlayout.marginHeight=20; //组件距容器边缘的高度为20像素 rowlayout.spacing=10; //组件之间的间距为10像素

shell.setLayout(rowlayout); //设置容器shell的布局方式为rowlayout Button bt1=new Button(shell,SWT.PUSH); //创建按钮 bt1.setText(\按钮1\);

RowData rowdata=new RowData(80,30); //创建布局数据类的对象 bt1.setLayoutData(rowdata); //设置按钮的布局数据 new Button(shell,SWT.PUSH).setText(\按钮2\); new Button(shell,SWT.PUSH).setText(\按钮3\); new Button(shell,SWT.PUSH).setText(\按钮4\); shell.pack(); //自动调整容器shell的大小 shell.open(); //打开主窗体

while(!shell.isDisposed()){ //如果主窗体没有关闭则一直循环 }

display.dispose(); //销毁display

if(!display.readAndDispatch()){ //如果display不忙 display.sleep(); //休眠 }

当rowlayout.pack=true时,各按钮为设定值,如图4.17所示;当rowlayout.pack=false时,各按钮被强制设定为相同大小,如图4.18所示;当rowlayout.justify=true时,将主窗体拉伸,各按钮间的距离也增大,但间隔均匀分布,如图4.19所示;当rowlayout.justify=false时,将主窗体拉伸,各按钮间距不变,如图4.20所示;当rowlayout.wrap=false时,当主窗体宽度收缩,按钮自动折行,如图4.21所示;当rowlayout.wrap=false时,主窗体宽度收缩,按钮不会折行,如图4.22所示。

图4.17 rowlayout.pack=true 图4.18 rowlayout.pack=false

图4.19 rowlayout.justify=true 图4.20 rowlayout.justify=false

图4.21 rowlayout.wrap=true 图4.22 rowlayout.wrap=false

4.4.3 网格式布局

网格式布局(GridLayout类)是实用而且功能强大的标准布局,也是较为复杂的一种布局。这种布局把容器分成网格,把组件放置在网格中。GridLayout有很多可配置的属性,和RowLayout一样,也有专用的布局数据类GridData, GridLayout的强大之处在于它可以通过GridData来设置每一个组件的外观形状。GridLayout的构造方法无参数,但可以通过GfidData和设置GridLayout的属性来设置组件的排列及组件的形状和位置。 1.GridLayout的属性

int numColumns:设置容器的列数,组件从左到右按列放置,当组件数大于列数时,下一个组件将自动添加新的一行。默认值为1列。

boolean makeColumnsEqualWidth:强制使列都具有相同的宽度,默认值为false。 int marginWidth:设置组件与容器边缘的水平距离,默认值为5。 int marginHeight:设置组件与容器边缘的垂直距离,默认值为5。 int horizontalSpacing:设置列与列之间的间隔,默认值为5。 int verticalSpacing:设置行与行之间的间隔,默认值为5。 例4.13 GridLayout的属性设置。

package edu.ch4;

import org.eclipse.swt.*;

import org.eclipse.swt.widgets.*; import org.eclipse.swt.layout.*; public class Sample4_13 {

public static void main(String[] args) {

Display display = new Display(); Shell shell = new Shell(display); shell.setText(\示例\);

GridLayout gridLayout = new GridLayout(); gridLayout.numColumns = 3; gridLayout.horizontalSpacing=30; gridLayout.makeColumnsEqualWidth=true; shell.setLayout(gridLayout);

new Button(shell, SWT.PUSH).setText(\);

}

}

new Button(shell, SWT.PUSH).setText(\超宽按钮 2\); new Button(shell, SWT.PUSH).setText(\按钮 3\); new Button(shell, SWT.PUSH).setText(\); new Button(shell, SWT.PUSH).setText(\按钮 5\); shell.pack(); shell.open();

while (!shell.isDisposed()) {

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

display.dispose();

当gridLayout.numColumns分别为1、2、3时,按钮依次按1列、2列和3列排列,运行结果如图4.23~4.25所示;当makeColumnsEqualWidth=true时,虽然按钮宽度不同,但列宽相同,如图4.26所示;当horizontalSpacing=30时,列间距为30,如图4.27所示。

图4.23 numColumns = 1 图4.24 numColumns = 2 图4.25 numColumns = 3

图4.26 makeColumnsEqualWidth=true 图4.27 horizontalSpacing=30

2.布局数据类(GridData类)

GridData是GridLayout专用的布局数据类,用GridData可以构建很多复杂的布局方式。① GridData的构造方法如下:

GridData(); 创建一个属性值为默认值的对象。

GridData(int type); 创建一个指定类型(type)的对象。 ② GridData常用类型如下:

GridData.FILL 通常与GridData类的对象属性horizontalAlignment和verticalAlignment配合使用,充满对象属性指定的空间。

GridData. FILL_HORIZONTAL 水平充满,组件充满网格水平方向的空间。 GridData. FILL_VERTICAL 垂直充满,组件充满网格垂直方向的空间。 GridData. FILL_BOTH 双向充满,组件充满水平和垂直方向的空间。

GridData.HORIZONTAL_ALIGN_BEGINNING 水平对齐靠左,组件在网格中靠左放置。 GridData.HORIZONTAL_ALIGN_CENTER 水平对齐居中,组件在网格中居中放置。 GridData.HORIZONTAL_ALIGN_END 水平对齐靠右,组件在网格中靠右放置。 ③ GridData常用对象属性如下:

int horizontalSpan 设置组件占用的列数,默认值为1。

int verticalSpan 设置组件占用的行数,默认值为1。 horizontalAlignment 设置组件的对齐方式为水平方向。 verticalAlignment 设置组件的对齐方式为垂直方向。 grabExcessHorizontalSpace 抢占额外的水平空间。 grabExcessVerticalSpace 抢占额外的垂直空间。

horizontalAlignment和verticalAlignment可以取以下值: BEGINNING 开始(水平对齐时居左;垂直对齐时居上) CENTER 居中

END 结束(水平对齐时居右;垂直对齐时居下) FILL 充满

默认的horizontalAlignment值是BEGINNING。默认的verticalAlignment值是CENTER。 例4.14 使用gridData布局。

package edu.ch4;

import org.eclipse.swt.SWT; import org.eclipse.swt.layout.*; import org.eclipse.swt.widgets.*; public class Sample4_14 {

public static void main(String[] args) {

Display display = new Display(); Shell shell = new Shell(display); shell.setText(\示例\);

GridLayout gridLayout = new GridLayout();//创建网格布局对象 gridLayout.numColumns = 3; //设置网格布局列数为3 gridLayout.makeColumnsEqualWidth=true; //强制列宽相等

shell.setLayout(gridLayout); //将shell设置为指定的网格布局式样 GridData gridData=new GridData(); //创建网格布局数据对象 gridData.horizontalSpan = 2; //水平方向跨2列 gridData.verticalSpan=2; //垂直方向跨2行

gridData.horizontalAlignment = GridData.CENTER; //水平方向居中 gridData.verticalAlignment = GridData.FILL; //垂直方向充满 Button b1=new Button(shell, SWT.PUSH); //创建按钮对象b1 b1.setText(\);

b1.setLayoutData(gridData); //将设定的网格布局数据用于按钮对象b1 new Button(shell, SWT.PUSH).setText(\超宽按钮 2\); new Button(shell, SWT.PUSH).setText(\按钮 3\); Button b4=new Button(shell, SWT.PUSH); b4.setText(\);

gridData = new GridData(GridData.FILL_HORIZONTAL);

b4.setLayoutData(gridData); //将gridData用于b4,水平方向充满 Button b5=new Button(shell, SWT.PUSH); b5.setText(\按钮 5\); gridData = new GridData();

gridData.horizontalAlignment = GridData.FILL;//设置b5为水平方向充满

//用带参数的构造方法创建gridData对象