draw2d的布局(加滚动条)

如图:


d

package test.draw2d.layout;

import org.eclipse.draw2d.BorderLayout;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.GridData;
import org.eclipse.draw2d.GridLayout;
import org.eclipse.draw2d.LightweightSystem;
import org.eclipse.draw2d.Panel;
import org.eclipse.draw2d.RectangleFigure;
import org.eclipse.draw2d.ScrollPane;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class LayoutShell extends Shell {

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			Display display = Display.getDefault();
			LayoutShell shell = new LayoutShell(display);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the shell.
	 * 
	 * @param display
	 */
	public LayoutShell(Display display) {
		super(display, SWT.SHELL_TRIM);
		createContents();
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("SWT Application draw2d");
		setSize(400, 400);
		setLayout(new FillLayout());

		LightweightSystem lws = new LightweightSystem(this);
		ScrollPane canvas = new ScrollPane();
		Panel panel = new Panel();
		createContent(panel);
		canvas.setContents(panel);
		lws.setContents(canvas);
	}

	private void createContent(Figure canvas) {
		BorderLayout borderLayout = new BorderLayout();
		borderLayout.setHorizontalSpacing(10);// BorderLayout分五块之间的间距
		borderLayout.setVerticalSpacing(10);// BorderLayout分五块之间的间距
		canvas.setLayoutManager(borderLayout);
		// ----------------GridData--left---right-------
		GridData gridDataLeftRight = new GridData();
		gridDataLeftRight.horizontalAlignment = SWT.FILL;
		gridDataLeftRight.verticalAlignment = SWT.FILL;
		gridDataLeftRight.grabExcessHorizontalSpace = true;
		gridDataLeftRight.grabExcessVerticalSpace = true;
		// ----------------left------------
		Figure canvasLeft = new Figure();
		canvasLeft.setSize(100, 0);// 有子元素按子元素的大小
		canvas.add(canvasLeft, BorderLayout.LEFT);

		canvasLeft.setLayoutManager(new GridLayout());
		RectangleFigure rectangleFigure = new RectangleFigure();
		rectangleFigure.setBackgroundColor(ColorConstants.yellow);
		rectangleFigure.setSize(50, 0);// 没有子元素按父元素的大小
		canvasLeft.add(rectangleFigure, gridDataLeftRight);
		// --------------right--------------
		Figure canvasRight = new Figure();
		canvasRight.setSize(100, 0);
		canvas.add(canvasRight, BorderLayout.RIGHT);

		canvasRight.setLayoutManager(new GridLayout());
		rectangleFigure = new RectangleFigure();
		rectangleFigure.setBackgroundColor(ColorConstants.yellow);
		rectangleFigure.setSize(50, 0);
		canvasRight.add(rectangleFigure, gridDataLeftRight);
		// ----------------GridData--top---bottom-------
		GridData gridDataTopBottom = new GridData();
		gridDataTopBottom.horizontalAlignment = SWT.FILL;
		gridDataTopBottom.verticalAlignment = SWT.FILL;
		gridDataTopBottom.grabExcessHorizontalSpace = true;
		gridDataTopBottom.grabExcessVerticalSpace = true;
		gridDataTopBottom.horizontalIndent = 100;
		// --------------top--------------
		Figure canvasTop = new Figure();
		canvasTop.setSize(0, 100);
		canvas.add(canvasTop, BorderLayout.TOP);

		canvasTop.setLayoutManager(new GridLayout());
		rectangleFigure = new RectangleFigure();
		rectangleFigure.setBackgroundColor(ColorConstants.blue);
		rectangleFigure.setSize(0, 50);
		canvasTop.add(rectangleFigure, gridDataTopBottom);
		// -------------bottom---------------
		Figure canvasBottom = new Figure();
		canvasBottom.setSize(0, 100);
		canvas.add(canvasBottom, BorderLayout.BOTTOM);

		canvasBottom.setLayoutManager(new GridLayout());
		rectangleFigure = new RectangleFigure();
		rectangleFigure.setBackgroundColor(ColorConstants.blue);
		rectangleFigure.setSize(0, 50);
		canvasBottom.add(rectangleFigure, gridDataTopBottom);
		// ------------center----------------
		// ScrollPane canvasCenter = new ScrollPane();
		Figure canvasCenter = new Figure();
		canvasCenter.setSize(550, 550);
		canvas.add(canvasCenter, BorderLayout.CENTER);

		canvasCenter.setLayoutManager(new GridLayout());
		rectangleFigure = new RectangleFigure();
		rectangleFigure.setBackgroundColor(ColorConstants.red);
		rectangleFigure.setSize(1000, 1000);
		canvasCenter.add(rectangleFigure, gridDataLeftRight);
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

}


参考:http://blog.csdn.net/zhaoxy_thu/article/details/9492931

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值