您的位置:首页 > 其它

SWT(JFace)体验之RowLayout布局

2009-06-25 11:14 741 查看

RowLayout布局 

相对于FillLayout来说,RowLayout比较灵活,功能也比较强。用户可以设置布局中子元素的大小、边距、换行及间距等属性。

RowLayout的风格

RowLayout中可以以相关的属性设定布局的风格,用户可以通过“RowLayout.属性”的方式设置RowLayout的布局风格,RowLayout中常用的属性如下。
Wrap:表示子组件是否可以换行(true为可换行)。
Pack:表示子组件是否为保持原有大小(true为保持原有大小)。
Justify:表示子组件是否根据父组件信息做调整。
MarginLeft:表示当前组件距离父组件左边距的像素点个数。
MarginTop:表示当前组件距离父组件上边距的像素点个数。
MarginRight:表示当前组件距离父组件右边距的像素点个数。
MarginBottom:表示当前组件距离父组件下边距的像素点个数。
Spacing:表示子组件之间的间距像素点个数。

另外,RowLayout可以通过RowData设置每个子组件的大小,例如“button.setLayoutData (new RowData(60, 60))”将设置button的大小为(60,60)。
测试代码:

复制代码 代码如下:
package swt_jface.demo2;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
public class RowLayoutSample {

Display display = new Display();
Shell shell = new Shell(display);
public RowLayoutSample() {

RowLayout rowLayout = new RowLayout();
// rowLayout.fill = true;
// rowLayout.justify = true;
// rowLayout.pack = false;
// rowLayout.type = SWT.VERTICAL;
// rowLayout.wrap = false;
shell.setLayout(rowLayout);

Button button1 = new Button(shell, SWT.PUSH);
button1.setText("button1");
button1.setLayoutData(new RowData(100, 35));

List list = new List(shell, SWT.BORDER);
list.add("item 1");
list.add("item 2");
list.add("item 3");

Button button2 = new Button(shell, SWT.PUSH);
button2.setText("button #2");

//shell.setSize(120, 120);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(String[] args) {
new RowLayoutSample();
}
}

再看看下面这个动态的(结合Composite)
复制代码 代码如下:
package swt_jface.demo2;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class Layouting {
Display display = new Display();
Shell shell = new Shell(display);
int count = 0;
public Layouting() {
shell.setLayout(new RowLayout());

final Composite composite = new Composite(shell, SWT.BORDER);
composite.setLayout(new RowLayout());
composite.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));
composite.addControlListener(new ControlListener() {
public void controlMoved(ControlEvent e) {
}
public void controlResized(ControlEvent e) {
System.out.println("Composite resize.");
}
});

Button buttonAdd = new Button(shell, SWT.PUSH);
buttonAdd.setText("Add new button");
buttonAdd.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
Button button = new Button(composite, SWT.PUSH);
button.setText("Button #" + (count++));
composite.layout(true);
composite.pack();
shell.layout(true);
shell.pack(true);
}
});

shell.pack();
//shell.setSize(450, 100);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(String[] args) {
new Layouting();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  SWT RowLayout 布局