测试:
。
package test.draw2d.layout;
import org.eclipse.draw2d.BorderLayout;
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);
Figure canvas = new Figure();
BorderLayout borderLayout = new BorderLayout();
borderLayout.setHorizontalSpacing(10);// BorderLayout分五块之间的间距
borderLayout.setVerticalSpacing(10);// BorderLayout分五块之间的间距
canvas.setLayoutManager(borderLayout);
lws.setContents(canvas);
// ----------------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----------------
Figure canvasCenter = new Figure();
// canvasCenter.setSize(0, 0);
canvas.add(canvasCenter, BorderLayout.CENTER);
canvasCenter.setLayoutManager(new GridLayout());
rectangleFigure = new RectangleFigure();
rectangleFigure.setBackgroundColor(ColorConstants.red);
// rectangleFigure.setSize(50, 40);
canvasCenter.add(rectangleFigure, gridDataLeftRight);
}
@Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}
}
。