如图:
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