SWT布局

  • 介绍
不管是在Android(如果只会用Eclipse自带的可视化编辑器的话,你根本称不上是会写Android)、Swing还是SWT中,布局都是非常重要的一部分。好像NetBeans也有Swing的可视化编辑器,但我非常不推荐使用——那样生成的代码完全无法维护,效率也是很低下的。 本文将会介绍如何使用SWT中的布局管理器,包括FillLayout、RowLayout、GridLayout等。通常,单一的布局管理器是无法满足实际需要的,而对于某一个布局管理器来说,通过无参构造函数返回的实例也是没什么用处的——参数是非常重要的一部分。简单来说,了解每个布局管理器的特性(参数的使用)以及如何相互配合是我们需要掌握的。 下面,我会依次介绍每个布局管理器,并给出示例代码以及截图:
  • FillLayout(填充布局)
[caption id="attachment_2676" align="aligncenter" width="194"] swt.FillLayout.simple swt.FillLayout.simple[/caption] [codesyntax lang="java"]
/**
 * Copyright By suren.
 * You can get more information from my website:
 * http://surenpi.com
 */
package org.suren.swt.test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 * @author suren
 * @date 2015年9月30日 下午2:59:05
 */
public class FillLayoutTest
{

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		Display display = new Display();
		Shell shell = new Shell(display);
		
		shell.setSize(200, 120);
		
		shell.setLayout(new FillLayout());
		
		new Button(shell, SWT.PUSH).setText("one");
		new Button(shell, SWT.PUSH).setText("two");
		new Button(shell, SWT.PUSH).setText("three");
		
		shell.open();
		while(!shell.isDisposed()){
			if(!display.readAndDispatch()){
				display.sleep();
			}
		}
		display.dispose();
	}

}
[/codesyntax] [caption id="attachment_2677" align="aligncenter" width="194"] swt.FillLayout.vertical.simple swt.FillLayout.vertical.simple[/caption] [codesyntax lang="java"]
/**
 * Copyright By suren.
 * You can get more information from my website:
 * http://surenpi.com
 */
package org.suren.swt.test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 * @author suren
 * @date 2015年9月30日 下午2:59:05
 */
public class FillLayoutTest
{

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		Display display = new Display();
		Shell shell = new Shell(display);
		
		shell.setSize(200, 120);
		
		shell.setLayout(new FillLayout(SWT.VERTICAL));
		
		new Button(shell, SWT.PUSH).setText("one");
		new Button(shell, SWT.PUSH).setText("two");
		new Button(shell, SWT.PUSH).setText("three");
		
		shell.open();
		while(!shell.isDisposed()){
			if(!display.readAndDispatch()){
				display.sleep();
			}
		}
		display.dispose();
	}

}
[/codesyntax] [caption id="attachment_2679" align="aligncenter" width="130"] swt.FillLayout.vertical.pack swt.FillLayout.vertical.pack[/caption] [codesyntax lang="java"]
/**
 * Copyright By suren.
 * You can get more information from my website:
 * http://surenpi.com
 */
package org.suren.swt.test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 * @author suren
 * @date 2015年9月30日 下午2:59:05
 */
public class FillLayoutTest
{

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		Display display = new Display();
		Shell shell = new Shell(display);
		
		shell.setSize(200, 220);
		
		shell.setLayout(new FillLayout(SWT.VERTICAL));
		
		new Button(shell, SWT.PUSH).setText("one");
		new Button(shell, SWT.PUSH).setText("two");
		new Button(shell, SWT.PUSH).setText("three");
		
		shell.pack();
		
		shell.open();
		while(!shell.isDisposed()){
			if(!display.readAndDispatch()){
				display.sleep();
			}
		}
		display.dispose();
	}

}
[/codesyntax] 从上面的几个例子里,我们可以总结出如下几点:FillLayout默认是横向布局的,可以通过type来设置为纵向,给容器内的元素平均分配空间,通过调用容器的pack方法可以让每个元素保持最合适的大小。
  • RowLayout(行布局)
[caption id="attachment_2681" align="aligncenter" width="144"] swt.RowLayout.simple swt.RowLayout.simple[/caption] [codesyntax lang="java"]
/**
 * Copyright By suren.
 * You can get more information from my website:
 * http://surenpi.com
 */
package org.suren.swt.test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 * @author suren
 * @date 2015年9月30日 下午3:53:59
 */
public class RowLayoutTest
{

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		Display display = new Display();
		Shell shell = new Shell(display);

		shell.setSize(150, 100);
		shell.setLayout(new RowLayout());
		
		new Button(shell, SWT.PUSH).setText("one");
		new Button(shell, SWT.PUSH).setText("two");
		new Button(shell, SWT.PUSH).setText("three");
		new Button(shell, SWT.PUSH).setText("four");
		new Button(shell, SWT.PUSH).setText("five");
		new Button(shell, SWT.PUSH).setText("six");
		
		shell.open();
		while(!shell.isDisposed()){
			if(!display.readAndDispatch()){
				display.sleep();
			}
		}
		display.dispose();
	}

}
[/codesyntax] [caption id="attachment_2682" align="aligncenter" width="138"] swt.RowLayout.wrap swt.RowLayout.wrap[/caption] [codesyntax lang="java"]
/**
 * Copyright By suren.
 * You can get more information from my website:
 * http://surenpi.com
 */
package org.suren.swt.test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 * @author suren
 * @date 2015年9月30日 下午3:53:59
 */
public class RowLayoutTest
{

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		Display display = new Display();
		Shell shell = new Shell(display);
		
		RowLayout rowLayout = new RowLayout();
		rowLayout.wrap = false;

		shell.setSize(150, 100);
		shell.setLayout(rowLayout);
		
		new Button(shell, SWT.PUSH).setText("one");
		new Button(shell, SWT.PUSH).setText("two");
		new Button(shell, SWT.PUSH).setText("three");
		new Button(shell, SWT.PUSH).setText("four");
		new Button(shell, SWT.PUSH).setText("five");
		new Button(shell, SWT.PUSH).setText("six");
		
		shell.open();
		while(!shell.isDisposed()){
			if(!display.readAndDispatch()){
				display.sleep();
			}
		}
		display.dispose();
	}

}
[/codesyntax] [caption id="attachment_2683" align="aligncenter" width="124"] swt.RowLayout.vertical swt.RowLayout.vertical[/caption] [codesyntax lang="java"]
/**
 * Copyright By suren.
 * You can get more information from my website:
 * http://surenpi.com
 */
package org.suren.swt.test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 * @author suren
 * @date 2015年9月30日 下午3:53:59
 */
public class RowLayoutTest
{

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		Display display = new Display();
		Shell shell = new Shell(display);
		
		RowLayout rowLayout = new RowLayout();
		rowLayout.wrap = false;
		rowLayout.type = SWT.VERTICAL;

		shell.setSize(150, 100);
		shell.setLayout(rowLayout);
		
		new Button(shell, SWT.PUSH).setText("one");
		new Button(shell, SWT.PUSH).setText("two");
		new Button(shell, SWT.PUSH).setText("three");
		new Button(shell, SWT.PUSH).setText("four");
		new Button(shell, SWT.PUSH).setText("five");
		new Button(shell, SWT.PUSH).setText("six");
		
		shell.open();
		while(!shell.isDisposed()){
			if(!display.readAndDispatch()){
				display.sleep();
			}
		}
		display.dispose();
	}

}
[/codesyntax] 从上面例子中,我们可以看到RowLayout按照行或者列来排列元素,满了的话会自动换行——除非设置了wrap为false。
  • GridLayout(格子布局)
[caption id="attachment_2685" align="aligncenter" width="143"] swt.GridLayout.simple swt.GridLayout.simple[/caption] [codesyntax lang="java"]
/**
 * Copyright By suren.
 * You can get more information from my website:
 * http://surenpi.com
 */
package org.suren.swt.test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

/**
 * @author suren
 * @date 2015年9月30日 下午4:21:32
 */
public class GridLayoutTest
{

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		Display display = new Display();
		Shell shell = new Shell(display);
		
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		
		shell.setSize(200, 200);
		shell.setLayout(gridLayout);
		
		new Button(shell, SWT.NULL).setText("one");
		new Button(shell, SWT.NULL).setText("two");
		new Button(shell, SWT.NULL).setText("three");
		new Button(shell, SWT.NULL).setText("four");
		
		Button label = new Button(shell, SWT.NULL);
		label.setText("five");
		label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 2, 1));
		
		shell.open();
		while(!shell.isDisposed()){
			if(!display.readAndDispatch()){
				display.sleep();
			}
		}
		display.dispose();
	}

}
[/codesyntax]
  • 滚动布局
想要在SWT中添加滚动条的话,需要用到org.eclipse.swt.custom.ScrolledComposite。个人感觉这个组件封装的不够好,查了很多资料才搞定,尤其要向容器中动态添加组件。下面给出代码片段: [codesyntax lang="java"]
Composite parent = new Composite(container, SWT.NULL);
parent.setLayout(new FillLayout()); //父必须是FillLayout的布局

ScrolledComposite scrolledComposite = new ScrolledComposite(parent,
		SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
scrolledComposite.setExpandHorizontal(true);
scrolledComposite.setExpandVertical(true);

Composite composite = new Composite(scrolledComposite, SWT.NULL);
scrolledComposite.setContent(composite); //这样就让Scroll来管理该组件

Point size = composite.computeSize(SWT.DEFAULT, SWT.DEFAULT); //通过计算出来的大小是最适合组件大小的
scrolledComposite.setMinSize(size);

//如果是动态向composite中添加组件的话,还需要调用下面的代码
composite.layout(true);
scrolledComposite.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
[/codesyntax]

转载于:https://my.oschina.net/surenpi/blog/816735

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值