手机
当前位置:查字典教程网 >编程开发 >Java >SWT(JFace)体验之RowLayout布局
SWT(JFace)体验之RowLayout布局
摘要:RowLayout布局相对于FillLayout来说,RowLayout比较灵活,功能也比较强。用户可以设置布局中子元素的大小、边距、换行及...

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(JFace)体验之RowLayout布局】相关文章:

java学习:日期的运算代码

SWT(JFace)体验之圆环状(戒指型)

Java 中实现随机无重复数字的方法

java通过url读取文件内容示例

简单学习Java抽象类要点及实例

深入Java Final

java基本教程之Thread中start()和run()的区别 java多线程教程

java操作mongodb实现CURD功能实例

JAVA hashCode使用方法详解

java中读取配置文件中数据的具体方法

精品推荐
分类导航