FlowLayout 以行的方式排列所有其管理之中的Figure;提供调整Figure的对齐方式和间距的方法。可对照AWT中的FlowLayout。
package com.jfans.layout;
import org.eclipse.draw2d.Cursors;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FlowLayout;
import org.eclipse.draw2d.GroupBoxBorder;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LightweightSystem;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
//Draw2D之FlowLayout
public class FlowLayoutDemo {
protected Shell shell;
/**
* Launch the application
* @param args
*/
public static void main(String[] args) {
try {
FlowLayoutDemo window = new FlowLayoutDemo();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window
*/
public void open() {
final Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
/**
* Create contents of the window
*/
protected void createContents() {
//Shell是Canvas的子类,而Canvas是Composite的子类
shell = new Shell();
shell.setSize(500, 375);
shell.setText("SWT Application");
//创建LightweightSystem,放在shell上
LightweightSystem lws = new LightweightSystem(shell);
//创建应用程序中的最顶层图形
IFigure panel = new Figure();
//FlowLayout
FlowLayout flowLayout = new FlowLayout(true);//水平方向
flowLayout.setMinorSpacing(80);
flowLayout.setMajorAlignment(FlowLayout.ALIGN_LEFTTOP);
panel.setLayoutManager(flowLayout);
/*panel.setLayoutManager(new FlowLayout(false));*///垂直方向
//把这个图形放置于LightweightSystem的RootFigure里
lws.setContents(panel);
//创建应用程序中的其他图形,并放置于应用程序的顶层图形中
for(int i=0; i<10; i++){
Label label = new Label(String.valueOf(i));
panel.add(label);
}
}
}