SWT界面布局介绍之FillLayout

给控件设置FillLayout会使子控件充满父控件的整个空间。FillLayout是SWT布局中最简单的一个,它将所有窗口组件以相同尺寸放置到一行或者一列中,不能换行,也不能自定义边框和距离。下面用一个简单模拟的工具箱实例来演示。

注意:默认情况下设置布局为FillLayout是横向排列的,在创建布局时构造函数传入布局样式即可显式声明布局排列方式:new FillLaout(SWT.HORIZONTAL)  new FillLaout(SWT.VERTICAL)

截图:

代码:

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

/**
 * SWT FillLayout布局使用demo 简易信息管理
 * @author xwalker
 *
 */
public class FillLayoutDemo{
	private Shell shell;
	private Table dataTable;
	private Text nameText;
	
	public void open() {
		Display display = Display.getDefault();
		createContents();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}
	/**
	 * 创建窗口组件
	 */
	protected void createContents(){
		shell=new Shell(SWT.MIN|SWT.CLOSE);
		shell.setText("简易信息管理");
		shell.setSize(300, 576);
		shell.setLayout(new FillLayout(SWT.VERTICAL));
		
		Composite compositeUp = new Composite(shell, SWT.NONE);
		compositeUp.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		dataTable = new Table(compositeUp, SWT.BORDER | SWT.FULL_SELECTION);
		dataTable.setHeaderVisible(true);
		dataTable.setLinesVisible(true);
		
		TableColumn tblclmnId = new TableColumn(dataTable, SWT.NONE);
		tblclmnId.setWidth(39);
		tblclmnId.setText("ID");
		
		TableColumn tblclmnName = new TableColumn(dataTable, SWT.NONE);
		tblclmnName.setWidth(100);
		tblclmnName.setText("Name");
		
		TableColumn tblclmnSex = new TableColumn(dataTable, SWT.NONE);
		tblclmnSex.setWidth(100);
		tblclmnSex.setText("Sex");
		
		TableColumn tblclmnAge = new TableColumn(dataTable, SWT.NONE);
		tblclmnAge.setWidth(100);
		tblclmnAge.setText("Age");
		
		Composite compositeDoen = new Composite(shell, SWT.NONE);
		compositeDoen.setLayout(null);
		
		Group group = new Group(compositeDoen, SWT.NONE);
		group.setText("新增");
		group.setBounds(10, 10, 274, 254);
		group.setLayout(null);
		
		Button cancelBtn = new Button(group, SWT.NONE);
		cancelBtn.setBounds(166, 198, 61, 27);
		cancelBtn.setText("重置");
		
		Button addBtn = new Button(group, SWT.NONE);
		addBtn.setBounds(66, 198, 61, 27);
		addBtn.setText("新增");
		
		Label birthdayLabel = new Label(group, SWT.NONE);
		birthdayLabel.setBounds(10, 123, 53, 17);
		birthdayLabel.setText("Birthday");
		
		DateTime birthdayDate = new DateTime(group, SWT.BORDER);
		birthdayDate.setBounds(85, 123, 95, 24);
		
		Label sexLabel = new Label(group, SWT.NONE);
		sexLabel.setBounds(10, 77, 53, 17);
		sexLabel.setText("Sex");
		
		Button sexMRadio = new Button(group, SWT.RADIO);
		sexMRadio.setBounds(85, 77, 42, 17);
		sexMRadio.setText("男");
		
		Button sexFmRadio = new Button(group, SWT.RADIO);
		sexFmRadio.setBounds(138, 77, 42, 17);
		sexFmRadio.setText("女");
		
		nameText = new Text(group, SWT.BORDER);
		nameText.setBounds(85, 33, 179, 23);
		
		Label nameLabel = new Label(group, SWT.NONE);
		nameLabel.setBounds(10, 33, 53, 17);
		nameLabel.setText("Name");
		insertDates(dataTable);
		shell.open();
	}

	private void insertDates(Table dataTable2) {
		TableItem item=null;
		for(int i=1;i<100;i++){
			item=new TableItem(dataTable, SWT.NONE);
			item.setText(new String[]{i+"","学生"+i,(i%2==0?"男":"女"),i+10+""});
		}
		
	}
	public static void main(String[] args) {
		FillLayoutLoginDemo demo=new FillLayoutLoginDemo();
		demo.open();
	}
}

转载于:https://my.oschina.net/imhoodoo/blog/102570

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值