SWT 中Button和Label重叠放置

本文展示了如何利用SWT(标准窗体工具包)创建一个包含多个复合组件和自定义布局的复杂窗口。通过调整复合组件的布局、背景色以及添加不同大小和图像的标签,实现了一个具有多部分划分和左右填充功能的窗口布局。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

写了一个小例子:

 

public class Snippet206 {

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

		Image image1 = display.getSystemImage(SWT.ICON_QUESTION);

		Image image2 = new Image(display,image1.getImageData().scaledTo(16, 16));
		Color color = new Color(null, 213, 222, 246);

		shell.setLayout(new GridLayout(3, false));
		new Composite(shell, SWT.BORDER).setLayoutData(new GridData(GridData.FILL_BOTH));
		new Composite(shell, SWT.BORDER).setLayoutData(new GridData(GridData.FILL_BOTH));
		new Composite(shell, SWT.BORDER).setLayoutData(new GridData(GridData.FILL_BOTH));

		new Composite(shell, SWT.BORDER).setLayoutData(new GridData(GridData.FILL_BOTH));

		Composite comp = new Composite(shell, SWT.BORDER);
		comp.setLayoutData(new GridData(GridData.FILL_BOTH));
		/* ******************************** */
		comp.setLayout(new FormLayout());
		FormData formData;

		// fill action to left
		int buttonSpace = 5;
		{
			Composite comp1 = new Composite(comp, SWT.NONE);
			formData = new FormData ();
			formData.top = new FormAttachment (0, buttonSpace);
			formData.left = new FormAttachment (0, buttonSpace);
			formData.bottom = new FormAttachment (100, -buttonSpace);
			comp1.setLayoutData (formData);
			comp1.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));

			{
				RowLayout rowLayout = new RowLayout(SWT.VERTICAL);
				rowLayout.spacing = 8;
				comp1.setLayout(rowLayout);

				Label label1 = new Label(comp1,SWT.NONE);
				label1.setImage(image2);
				Label label2 = new Label(comp1,SWT.NONE);
				label2.setImage(image2);
				Label label3 = new Label(comp1,SWT.NONE);
				label3.setImage(image2);
			}

			formData = new FormData ();
			formData.top = new FormAttachment (0, buttonSpace);
			formData.left = new FormAttachment (comp1, 0);
			formData.bottom = new FormAttachment (100, -buttonSpace);
			Label label2 = new Label(comp,SWT.SEPARATOR);
			label2.setLayoutData(formData);
		}
		// fill action to right
		{
			Composite comp1 = new Composite(comp, SWT.NONE);
			formData = new FormData ();
			formData.top = new FormAttachment (0, buttonSpace);
			formData.right = new FormAttachment (100, -buttonSpace);
			formData.bottom = new FormAttachment (100, -buttonSpace);
			comp1.setLayoutData (formData);
			comp1.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));

			{
				RowLayout rowLayout = new RowLayout(SWT.VERTICAL);
				rowLayout.spacing = 8;
				comp1.setLayout(rowLayout);

				Label label1 = new Label(comp1,SWT.NONE);
				label1.setImage(image2);
				Label label2 = new Label(comp1,SWT.NONE);
				label2.setImage(image2);
				Label label3 = new Label(comp1,SWT.NONE);
				label3.setImage(image2);
			}

			formData = new FormData ();
			formData.top = new FormAttachment (0, buttonSpace);
			formData.right = new FormAttachment (comp1, 0);
			formData.bottom = new FormAttachment (100, -buttonSpace);
			Label label2 = new Label(comp,SWT.SEPARATOR);
			label2.setLayoutData(formData);
		}

		Button button = new Button(comp, SWT.PUSH);
		button.setImage(display.getSystemImage(SWT.ICON_QUESTION));
		button.setText("Some Action");

		formData = new FormData ();
		formData.top = new FormAttachment (0, 0);
		formData.left = new FormAttachment (0, 0);
		formData.right = new FormAttachment (100, 0);
		formData.bottom = new FormAttachment (100, 0);
		button.setLayoutData (formData);


		/* ******************************** */

		new Composite(shell, SWT.BORDER).setLayoutData(new GridData(GridData.FILL_BOTH));;
		new Composite(shell, SWT.BORDER).setLayoutData(new GridData(GridData.FILL_BOTH));;
		new Composite(shell, SWT.BORDER).setLayoutData(new GridData(GridData.FILL_BOTH));;
		new Composite(shell, SWT.BORDER).setLayoutData(new GridData(GridData.FILL_BOTH));;

		shell.setSize(800, 560);
		shell.open();
		while (!shell.isDisposed ()) {
			if (!display.readAndDispatch ()) display.sleep ();
		}
		image2.dispose();
		color.dispose();
		display.dispose ();
	}
}

 

效果图:



 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值