java rowstyle_SWT(JFace)体验之RowLayout布局

本文详细介绍了RowLayout布局在Swing或 SWT 应用中的强大功能,包括子元素控制、风格设置、属性调整和动态组件添加。通过实例展示了如何使用Wrap、Pack、Justify等属性,并演示了如何通过RowData设置组件尺寸和响应布局变化。
摘要由CSDN通过智能技术生成

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();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值