gridlayout用法 java_SWT:GridLayout 的使用需要注意的事项

SWT当中的GridLayout是一个非常灵活的控件,但是在使用起来需要在控制上下一番功夫.

大家都知道,JAVA在写编写窗口程序的时候,物件的添加,放置 操作起来要比.net费劲的多,但是如果用好了相关org.eclipse.layout.*包当中的相关类,也会写出十分漂亮的界面程序.

下面大家先看一个程序:

源码如下:

package com.layout;

import org.eclipse.swt.widgets.*;

import org.eclipse.swt.layout.*;

import org.eclipse.swt.SWT;

public class CopyOfGridLayoutExc {

public static void main(String[] args) {

Display display = new Display();

Shell shell = new Shell(display);

shell.setText("Find (GridLayout)");

Label label = new Label(shell, SWT.NONE);

label.setText("Find what:");

Text text = new Text(shell, SWT.BORDER);

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

findButton.setText("Find Next");

Group group = new Group(shell, SWT.NONE);

group.setLayout(new RowLayout());

Button upButton = new Button(group, SWT.RADIO);

upButton.setText("Up");

Button downButton = new Button(group, SWT.RADIO);

downButton.setText("Down");

downButton.setSelection(true);

group.setText("Direction");

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

cancelButton.setText("Cancel");

/* Use a GridLayout to position the controls */

Monitor monitor = shell.getMonitor();

int width = monitor.getClientArea().width / 10;

GridLayout layout = new GridLayout(4, false);

layout.marginWidth = layout.marginHeight = 14;//layout leave's the window's space

shell.setLayout(layout);

GridData labelData =

new GridData(SWT.FILL, SWT.CENTER, false, false);

label.setLayoutData(labelData);

GridData textData =

new GridData(SWT.FILL,SWT.CENTER,true,false,2,1);

textData.widthHint = width;

text.setLayoutData(textData);

GridData findData =

new GridData(SWT.FILL, SWT.CENTER, false, false);

findButton.setLayoutData(findData);

GridData groupData =

new GridData(SWT.RIGHT,SWT.TOP,false,false,3,1);

group.setLayoutData(groupData);

GridData cancelData =

new GridData(SWT.FILL, SWT.TOP, false, false);

cancelButton.setLayoutData(cancelData);

shell.pack();

shell.open();

while (!shell.isDisposed()) {

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

}

display.dispose();

}

}

这其中我们在使用的时候应该要注意以下几点:

1.要选择自己适合 的Layout类型.GridLayout适合于多种情况,它大部分情况是使用在较为复杂的界面编程当中,因为复杂的界面会有相当多的控件.

2.GridData的使用将是一个控制界面显示的主要类.通过使用GridData我们可以很好的控制界面.

其中GridData的构造函数比较多,但是相关的使用我们都应该熟悉,特别是上面源程序当中使用的那个构造函数,在使用起来更容易控制GridLayout的布局.通过horizantalSpan,VerticalSpan来控制控件所占用的单元格,这样就会控制其它控制是否在一列当中显示还是在几列当中显示.前提是通过GridLayout.numColumns来设置列数.

3.如果不设置GridData那么相关的控件都会按照相关的建立顺序加入到GridLayout当中.GridData不能控制控件的显示顺序,而相关顺序是对象的建立顺序来控制的.这一点不要与GridData混淆了.

希望写这篇文章对大家学习SWT有用.

posted on 2006-09-12 10:50 水煮三国 阅读(2373) 评论(1)  编辑  收藏 所属分类: J2SE

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值