swt_jface(2) 几种常用布局

swt_jface(2) 几种常用布局

// RowLayoutTest .java
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
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;

public class RowLayoutTest {

 /**
  * @param args
  */
 public static void main(String[] args) {
  Display display=new Display();
  Shell shell=new Shell();
  shell.setText("标题");
  shell.setSize(400,300);
  
  
//RowLayout布局就是是组件按行排列
  //shell.setLayout(new RowLayout());
  
  //组件
  new Button(shell,SWT.NONE).setText("kkk");
  new Button(shell, SWT.NONE).setText("确定");
  new Button(shell,SWT.NONE).setText("kk1");
  new Button(shell, SWT.NONE).setText("确1");
  new Button(shell,SWT.NONE).setText("kk2");
  new Button(shell, SWT.NONE).setText("确2");
  new Button(shell,SWT.NONE).setText("kkk");
  new Button(shell, SWT.NONE).setText("确定");
  new Button(shell,SWT.NONE).setText("kk1");
  new Button(shell, SWT.NONE).setText("确1");
  new Button(shell,SWT.NONE).setText("kk2");
  new Button(shell, SWT.NONE).setText("确2");
  new Button(shell,SWT.NONE).setText("kkk");
  new Button(shell, SWT.NONE).setText("确定");
  new Button(shell,SWT.NONE).setText("kk1");
  new Button(shell, SWT.NONE).setText("确1");
  new Button(shell,SWT.NONE).setText("kk2");
  new Button(shell, SWT.NONE).setText("确2");
  
  

  RowLayout layout = new RowLayout( /*SWT.VERTICAL垂直排列*/ );
  layout.marginWidth = 20;
  layout.marginHeight = 10;
  layout.spacing = 15;
  //垂直排列
  //layout.type = SWT.VERTICAL;

   //设置布局管理器上的组件大小相同
  //layout.pack = false;
  
  // 设置布局管理器上的组件根据容器空间可以拉伸
  layout.justify = true;
  
    //不自动换行
   // layout.wrap = false;

   
  new Button(shell, SWT.NONE).setText("确定");
  
  Button b = new Button(shell, SWT.NONE);
  b.setText("取消");
  // 使用RowData布局数据类来控制按钮,使按钮改为50像素宽,30像素长
  RowData rowData = new RowData(50, 30);  
  // 把组件隐藏不占位,相当于组件不存在
  //rowData.exclude = true;
  rowData.width = 100;
  //把组件隐藏,但位置还占着
  //b.setVisible(false);
  b.setLayoutData(rowData);
  
  new Button(shell, SWT.NONE).setText("帮助");
  
  shell.setLayout(layout);
  
  
  shell.open();
  while(!shell.isDisposed()){
   if(!display.readAndDispatch()){
    display.sleep();
   }
  }
  display.dispose();
 }

}


//GridLayoutTest
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class GridLayoutTest {

 /**
  * @param args
  */
 /**
  * @param args
  */
 public static void main(String[] args) {
  Display display = new Display();
  Shell shell = new Shell();
  shell.setText("标题");
  shell.setSize(400, 300);

  //GridLayout为网格布局
  //4代表把这个布局分成几列
  //false代表是否等距分隔空间
  shell.setLayout(new GridLayout(4, true));

  // 组建
  new Button(shell, SWT.NONE).setText("kkk");
  new Button(shell, SWT.NONE).setText("确定");
  new Button(shell, SWT.NONE).setText("kk1");
   new Button(shell, SWT.NONE).setText("确1");
   new Button(shell,SWT.NONE).setText("kk2");
   new Button(shell, SWT.NONE).setText("确2");
   new Button(shell, SWT.NONE).setText("kkk");
   new Button(shell, SWT.NONE).setText("确定");
   new Button(shell, SWT.NONE).setText("kk1");
   new Button(shell, SWT.NONE).setText("确1");
   new Button(shell,SWT.NONE).setText("kk2");
   new Button(shell, SWT.NONE).setText("确2");
  // 定义一个GridData对象,让帮助按钮占用n列的空间
   Button helpButton = new Button(shell, SWT.NONE);
   
   //GridData.FILL_VERTICAL是按钮垂直放置
   GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL/*这个是水平对齐式填充*//* GridData.FILL_HORIZONTAL 这个是水平抢占式填充*//* GridData.FILL_VERTICAL*/);
   gridData.horizontalSpan = 3;//该句使按钮占用两列空间
   //gridData.grabExcessHorizontalSpace=true;
   
   //gridData.verticalSpan = 2;
   //gridData.grabExcessVerticalSpace=true;
   helpButton.setLayoutData(gridData);
   
   helpButton.setText("帮助");

    new Button(shell, SWT.NONE).setText("kk1");
    new Button(shell, SWT.NONE).setText("确1");
    new Button(shell,SWT.NONE).setText("kk2");
    new Button(shell, SWT.NONE).setText("确2");
  
  
  
  shell.open();
  while (!shell.isDisposed()) {
   if (!display.readAndDispatch()) {
    display.sleep();
   }
  }
  display.dispose();

 }

}

//FillLayoutTest

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

public class FillLayoutTest {

 
 public static void main(String[] args) {
  Display display=new Display();
  Shell shell=new Shell();
  shell.setText("标题");
  shell.setSize(400,300);
  
  //布局FillLayout就是使组建占满整个容器 默认为横着排  SWT.VERTICAL是组建垂直排
//  shell.setLayout(new FillLayout(SWT.VERTICAL));
  shell.setLayout(new FillLayout());
  
  //组建
  new Button(shell,SWT.NONE).setText("kkk");
  new Button(shell, SWT.NONE).setText("确定");
  new Button(shell,SWT.NONE).setText("kk1");
//  new Button(shell, SWT.NONE).setText("确1");
//  new Button(shell,SWT.NONE).setText("kk2");
//  new Button(shell, SWT.NONE).setText("确2");
  shell.open();
  while(!shell.isDisposed()){
   if(!display.readAndDispatch()){
    display.sleep();
   }
  }
  display.dispose();
  
  
  
 }

}

你可能感兴趣的:(swt_jface(2) 几种常用布局)