SWT&JFace 常用布局

常用布局管理器

Java的布局:FlowLayout、BorderLayout、GridLayout、GridBagLayout、CardLayout、BoxLayout
 Android的布局:LinearLayout 、TableLayout、RelativeLayout、FrameLayout、AbsoluteLayout

1. FillLayout

填充式布局管理器,即组件的大小会尽可能充满整个容器。
构造函数:

public FillLayout()
public FillLayout(int type)
eg: shell.setLayout(new FillLayout());

package plugin;

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 LayoutTest {
  public static void main(String[] args) {
    final Display display = Display.getDefault();
    final Shell shell = new Shell();
    shell.setSize(500, 180);
    shell.setText("FillLayout布局");
    shell.setLayout(new FillLayout());
    Button button1 = new Button(shell, SWT.NONE);
    Button button2 = new Button(shell, SWT.NONE);
    button1.setText("button1");
    button2.setText("button2");
    shell.open();
    while(!shell.isDisposed()){
      if(!display.readAndDispatch()){
      display.sleep();
      }
    }
    display.dispose();
  }
} 
2. RowLayout

行|列布局管理器,不同的组件大小可以不同,可以设置边界距离和组件间距
构造函数:

public RowLayout()
public RowLayout(int type)

package plugin;

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;

public class TestRowLayout {

  public static void main(String[] args) {
    final Display display = Display.getDefault();
    final Shell shell = new Shell();
    shell.setSize(500, 180);
    shell.setText("FillLayout布局");
    RowLayout rowLayout = new RowLayout();
    rowLayout.marginTop = 20;
    rowLayout.spacing = 10;
    shell.setLayout(rowLayout);

    Button button1 = new Button(shell, SWT.NONE);
    Button button2 = new Button(shell, SWT.NONE);
    Button button3 = new Button(shell, SWT.NONE);
    Button button4 = new Button(shell, SWT.NONE);
    Button button5 = new Button(shell, SWT.NONE);
    button1.setText("button1");
    button2.setText("button2");
    button3.setText("button2");
    button4.setText("button2");
    button5.setText("button2");
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}
3. GridLayout

网格布局管理器,一般配合使用其专用的布局数据类GridData
构造函数:

public GridLayout(int numColumns,boolean makeColumnsEqualWidth)
numColumns:表格列数
makeColumnsEqualWidth:指定列宽是否相等

package plugin;

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 TestGridLayout {
  public static void main(String[] args) {
    final Display display = Display.getDefault();
    final Shell shell = new Shell();
    shell.setSize(240,180);
    shell.setText("GridLayout布局");

    GridLayout gridLayout = new GridLayout();
    gridLayout.numColumns = 3;
    gridLayout.marginTop= 10;
    gridLayout.marginBottom = 10;
    gridLayout.horizontalSpacing = 10;
    gridLayout.marginRight = 10;
    gridLayout.makeColumnsEqualWidth = true;
    shell.setLayout(gridLayout);

    GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
    gridData.widthHint=60;
    gridData.heightHint=100;
    gridData.verticalSpan=2;

    Button button1 = new Button(shell, SWT.NONE);
    button1.setText("按鈕1");
    button1.setLayoutData(gridData);

    gridData = new GridData(GridData.FILL_BOTH);  
    Button button2 = new Button(shell, SWT.NONE);
    button2.setText("按鈕2");
    button2.setLayoutData(gridData); 

    gridData = new GridData(GridData.FILL_BOTH);
    Button button3 = new Button(shell, SWT.NONE);
    button3.setText("按鈕3");
    button3.setLayoutData(gridData);

    gridData = new GridData(GridData.FILL_BOTH);  
    Button button4 = new Button(shell, SWT.NONE);
    button4.setText("按鈕4");
    button4.setLayoutData(gridData);

    gridData = new GridData(GridData.FILL_BOTH);  
    Button button5 = new Button(shell, SWT.NONE);
    button5.setText("按鈕5");
    button5.setLayoutData(gridData);

    shell.open();

    while(!shell.isDisposed()){
      if(!display.readAndDispatch()){
        display.sleep();
      }
    }
  }
}
4. FormLayout

自定义布局管理器

package plugin;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class TestFormLayout {
  public static void main(String[] args) {
    final Display display = Display.getDefault();
    final Shell shell = new Shell();
    shell.setText("FormLayout布局");
    shell.setSize(300, 180);
    FormLayout layout = new FormLayout();
    layout.marginTop = 5;
    layout.marginLeft = 5;
    layout.marginRight = 5;
    layout.marginBottom = 5;
    shell.setLayout(layout);
    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("按钮1");
    FormData formData = new FormData();
    formData.height = 40;

    formData.right = new FormAttachment(100, -100);
    formData.left = new FormAttachment(0, 10);
    formData.left = new FormAttachment(0, 10);
    formData.top = new FormAttachment(0, 10,0);
    button1.setLayoutData(formData);

    Button button2 = new Button(shell,SWT.PUSH);
    button2.setText("按钮2");
    formData = new FormData();
    formData.height = 60;
    formData.width = 90;
    button2.setLayoutData(formData);
    formData.top = new FormAttachment(button1,5,SWT.BOTTOM);
    formData.left = new FormAttachment(button1,-10,SWT.RIGHT);

    shell.open();

    while(!shell.isDisposed()){
      if(!display.readAndDispatch()){
      display.sleep();
      }
    }
    display.dispose();
  }
}

你可能感兴趣的:(SWT&JFace 常用布局)