Java Tutorial/SWT/FillLayout

Материал из Java эксперт
Перейти к: навигация, поиск

Configure the layout orientation: HORIZONTAL and VERTICAL

The possible values for type: SWT.HORIZONTAL, SWT.VERTICAL

SWT.HORIZONTAL: lays the controls in a single row SWT.VERTICAL: lays the controls in a single column.



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 FillLayoutHorizontal {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout(SWT.HORIZONTAL));
    new Button(shell, SWT.PUSH).setText("one");
    new Button(shell, SWT.PUSH).setText("two");
    new Button(shell, SWT.PUSH).setText("three");
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Define the margins and spacing properties

The default (initial) value of each property is 0.



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 MarginSpaceProperties {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    FillLayout fillLayout = new FillLayout(SWT.HORIZONTAL);
    // Width of horizontal margins placed along the left and right edges    
    fillLayout.marginWidth = 5;
    // Height of vertical margins placed along the top and bottom edges
    fillLayout.marginHeight = 5;
    
    shell.setLayout(fillLayout);
    
    
    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("button1");
    Button button2 = new Button(shell, SWT.PUSH);
    button2.setText("button number 2");
    Button button3 = new Button(shell, SWT.PUSH);
    button3.setText("3");
    
    
    shell.setSize(450, 100);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Set spacing properties

The default (initial) value of each property is 0.



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 LayoutSpaceProperties {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    FillLayout fillLayout = new FillLayout(SWT.HORIZONTAL);
    
    fillLayout.marginWidth = 5;
    fillLayout.marginHeight = 5;
    
    // Number of pixels between the edge of a cell and edges of its neighboring cells
    fillLayout.spacing = 10;    
    
    shell.setLayout(fillLayout);
    
    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("button1");
    Button button2 = new Button(shell, SWT.PUSH);
    button2.setText("button number 2");
    Button button3 = new Button(shell, SWT.PUSH);
    button3.setText("3");
    
    shell.setSize(450, 100);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Using FillLayout

  1. FillLayout lays out controls into a single row or column (without wrap).
  2. FillLayout forces all the children to be the same size.
  3. FillLayout places the children into the whole client area of a composite.

You can specify the margins and spacing properties. FillLayout has no layout data. The primary usage of FillLayouts is laying out controls in task bars and toolbars.

It places all controls in either a single column or a single row, and makes them all the same size.

FillLayout Constructors:

ConstructorDescriptionpublic FillLayout()Constructs a FillLayout and sets type to SWT.HORIZONTAL.public FillLayout(int type)Constructs a FillLayout and sets type to the passed type.