java swt gridlayout_SWT界面布局介绍之GridLayout

GridLayout网格布局时最常用的布局之一,类似于Html中使用table布局。通过使用GridData来设置布局参数。

做个例子看看。

8287952e6bc6967364d84ba85358ab19.gif

代码:

import org.eclipse.swt.SWT;

import org.eclipse.swt.layout.FillLayout;

import org.eclipse.swt.layout.GridData;

import org.eclipse.swt.layout.GridLayout;

import org.eclipse.swt.widgets.Button;

import org.eclipse.swt.widgets.Composite;

import org.eclipse.swt.widgets.Display;

import org.eclipse.swt.widgets.Group;

import org.eclipse.swt.widgets.Label;

import org.eclipse.swt.widgets.List;

import org.eclipse.swt.widgets.Shell;

import org.eclipse.swt.widgets.Text;

/**

* SWT FillLayout布局使用demo 发邮件

* @author xwalker

*

*/

public class GridLayoutDemo{

private Shell shell;

private Text toAddrText;

private Text topicText;

private Text ccText;

private Text labelText;

private Button sendBtn;

private Button timSendBtn;

private Button saveBtn;

private Composite composite;

private Group group;

public void open() {

Display display = Display.getDefault();

createContents();

while (!shell.isDisposed()) {

if (!display.readAndDispatch()) {

display.sleep();

}

}

}

/**

* 创建窗口组件

*/

protected void createContents(){

shell=new Shell();

shell.setText("发邮件");

shell.setSize(800, 450);

shell.setLayout(new GridLayout(5, false));

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

toLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));

toLabel.setText("收件人");

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

toAddrText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1));

composite = new Composite(shell, SWT.NONE);

composite.setLayout(new FillLayout(SWT.HORIZONTAL));

GridData gd_composite = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 4);

gd_composite.widthHint = 171;

composite.setLayoutData(gd_composite);

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

group.setText("联系人");

group.setLayout(new FillLayout(SWT.HORIZONTAL));

List list = new List(group, SWT.BORDER);

insertData(list);

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

topiclabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));

topiclabel.setText("主题");

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

topicText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1));

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

ccLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));

ccLabel.setText("抄送");

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

ccText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1));

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

contentLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));

contentLabel.setText("内容");

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

GridData gd_labelText = new GridData(SWT.FILL, SWT.FILL, true, false, 3, 1);

gd_labelText.heightHint = 232;

labelText.setLayoutData(gd_labelText);

new Label(shell, SWT.NONE);

sendBtn = new Button(shell, SWT.NONE);

sendBtn.setText("发送");

timSendBtn = new Button(shell, SWT.NONE);

timSendBtn.setText("定时发送");

saveBtn = new Button(shell, SWT.NONE);

saveBtn.setText("存草稿");

new Label(shell, SWT.NONE);

shell.open();

}

private void insertData(List list) {

for(int i=1;i<=20;i++){

list.add("联系人"+i);

}

}

public static void main(String[] args) {

GridLayoutDemo demo=new GridLayoutDemo();

demo.open();

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值