现在的位置: 首页 > 综合 > 正文

SWT RowLayout 行布局

2017年03月17日 ⁄ 综合 ⁄ 共 3635字 ⁄ 字号 评论关闭
RowLayout
RowLayoutFillLayout相似,它们放置全部的组件在单行或单列.但是RowLayout没有强制全部的组件相同的大小.它能当组件在行或列上超出父组件空间时自动换行.
RowLayout使用RowData来决定组件的初始化大小(宽和高).你可以传递RowData对象给组件的setLayoutData()方法.布局管理器(RowLayout)通过组件的RowData来决定组件的大小和位置.
注意: SWT组件中,有一个方法名为setData()名字有点像setLayoutDate().如果你设置了控件的相关属性,却没有达到你想要的效果,请确认你使用的是setLayoutData()方法.

RowData类有两个公共的数据成员:
public int height
public int width

你可以设置这些值在创建了
RowData对象后,例如下面的代码建立一个Button和设置它的大小为100像数宽和50像数高:
Button button = new Button(shell, SWT.PUSH);
RowData rowData = new RowData();
rowData.height = 50;
rowData.width = 100;
button.setLayoutData(rowData); 

RowData提供两个构造函数来指定高和宽.

构造函数 描述
public RowData(int width, int height) 单独的设置宽和高
public RowData(Point point) new Point(100, 50),通过Point设置宽和高


RowLayout和FillLayout相似,也提供属性type(SWT.HORIZONTAL或SWT.VERTICAL)来配置布局为行或列.RowLayout同时也提供另外几个属性来定制布局风格. 

RowLayout的属性

属性 描述
boolean justify 如果为true,类似于平均分配当前行或行上的空间.默认为false.
int marginBottom, marginLeft, marginRight, marginTop 分别定义底,左,右,上边距,以像素为单位.默认为3像素.
boolean pack 如果为true,通知控件调整为它自已最合适的大小,默认为true.
int spacing 设置邻近控件间的间距,像素为单位,默认为3像素.
int type 布局管理器中控件布局的方式,默认为SWT.HORIZONTAL按行排列,SWT.VERTICAL时按列排列.
boolean warp 如果为true,当行或列的超出布局空间大小时自动换行到下一行或列.默为认true.
boolean fill         当水平填充时,子控件是否等高,当垂直填充时,子控件是否等宽.默认为false.

.RowLayout有两个构造函数,一个为空,一个为带单参数type.
下面是一个简单的RowLayout布局程序运行的示例:
o_RowLayout-1.jpg

Example:

package chapter4;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 * @author HexUzHoNG
 *
 
*/

public class RowLayoutDemo {

    
public static void main(String[] args) {
        
        Display display 
= new Display();
        Shell shell 
= new Shell(display);
        
        RowLayout rowLayout 
= new RowLayout();
        
//可以通过设置相关属性来改变外观 :-)
        rowLayout.justify = true;
        
        shell.setLayout(rowLayout);
        
        
new Button(shell, SWT.PUSH).setText("one");
        
new Button(shell, SWT.PUSH).setText("two");
        
new Button(shell, SWT.PUSH).setText("three");
        
new Button(shell, SWT.PUSH).setText("four");
        
new Button(shell, SWT.PUSH).setText("five");
        
new Button(shell, SWT.PUSH).setText("six");
        
        shell.open();
        
        
while (!shell.isDisposed()) {
            
if (!display.readAndDispatch()) {
                display.sleep();
            }

        }

        display.dispose();
    }


}

RowLayoutRowData一起使用,可以在相当大程度上改变程序外观,下面是一个例子:
r_RowLayout-2.jpg

Example:

package chapter4; 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.Shell; /**  * @author HexUzHoNG  *  */ public class RowLayoutDemo {     public static void main(String[] args) {                  Display display = new Display();         Shell shell = new Shell(display);                  RowLayout rowLayout = new RowLayout(SWT.VERTICAL);         rowLayout.marginLeft = 10;                  shell.setLayout(rowLayout);                  new Button(shell, SWT.PUSH).setText("one");         new Button(shell, SWT.PUSH).setText("two");         new Button(shell, SWT.PUSH).setText("three");         new Button(shell, SWT.PUSH).setText("four");         new Button(shell, SWT.PUSH).setText("five");         new Button(shell, SWT.PUSH).setText("six");                  Button seven = new Button(shell, SWT.PUSH);         RowData rowDate = new RowData(8080);         seven.setLayoutData(rowDate);         seven.setText("seven");                  shell.open();                  while (!shell.isDisposed()) {             if (!display.readAndDispatch()) {                 display.sleep();             }         }         display.dispose();     } }

抱歉!评论已关闭.