Java Tutorial/SWT/RowLayout

Материал из Java эксперт
Версия от 15:22, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

pack: Specifies whether all controls should take their preferred size

If pack is set to true, all controls have the same size, which is the size required to accommodate the largest preferred height and the largest preferred width of all the controls. The default value is true.



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.List;
import org.eclipse.swt.widgets.Shell;
public class RowlLayoutPack {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    RowLayout rowLayout = new RowLayout();
    rowLayout.pack = false;
    
    shell.setLayout(rowLayout);
    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("button1");
    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(450, 100);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





RowLayout: align widgets in a row

/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
//package org.eclipse.swt.snippets;
/*
 * RowLayout snippet: align widgets in a row
 * 
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 * 
 * @since 3.1
 */
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;
public class RowLayoutAlignWidgets {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    RowLayout layout = new RowLayout(SWT.HORIZONTAL);
    layout.wrap = true;
    layout.fill = false;
    layout.justify = true;
    shell.setLayout(layout);
    Button b = new Button(shell, SWT.PUSH);
    b.setText("Button 1");
    b = new Button(shell, SWT.PUSH);
    b.setText("Button 2");
    b = new Button(shell, SWT.PUSH);
    b.setText("Button 3");
    b = new Button(shell, SWT.PUSH);
    b.setText("Not shown");
    b.setVisible(false);
    RowData data = new RowData();
    data.exclude = true;
    b.setLayoutData(data);
    b = new Button(shell, SWT.PUSH);
    b.setText("Button 200 high");
    data = new RowData();
    data.height = 200;
    b.setLayoutData(data);
    b = new Button(shell, SWT.PUSH);
    b.setText("Button 200 wide");
    data = new RowData();
    data.width = 200;
    b.setLayoutData(data);
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





RowLayout: fill

Specifies whether all the controls in a row should share the same height if the type of this layout is HORIZONTAL. Or, whether all the controls in a column should share the same width. The default value is false.



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.List;
import org.eclipse.swt.widgets.Shell;
public class RowLayoutFill {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    RowLayout rowLayout = new RowLayout();
    rowLayout.fill = true; // Overriding default values.
    
    shell.setLayout(rowLayout);
    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("button1");
    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(450, 100);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





RowLayout: justify

Specifies whether the controls in a row should be justified. The default value is false.



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.List;
import org.eclipse.swt.widgets.Shell;
public class RowLayoutJustify {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    RowLayout rowLayout = new RowLayout();
    rowLayout.justify = true;
    
    shell.setLayout(rowLayout);
    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("button1");
    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(450, 100);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





RowLayout: marginLeft, marginRight, marginTop, marginBottom

The default value for each of the margins is 3.



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.List;
import org.eclipse.swt.widgets.Shell;
public class RowLayoutMargin {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    RowLayout rowLayout = new RowLayout();
    
    rowLayout.marginLeft=20;
    rowLayout.marginRight=20;
    rowLayout.marginTop=20;
    rowLayout.marginBottom=20;
    
    shell.setLayout(rowLayout);
    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("button1");
    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(450, 100);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





RowLayout snippet: align widgets in a column

/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
//package org.eclipse.swt.snippets;
/*
 * RowLayout snippet: align widgets in a column
 * 
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 * 
 * @since 3.1
 */
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;
public class RowLayoutAlignColumn {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    RowLayout layout = new RowLayout(SWT.HORIZONTAL);
    layout.wrap = true;
    layout.fill = true;
    layout.justify = false;
    shell.setLayout(layout);
    Button b = new Button(shell, SWT.PUSH);
    b.setText("Button 1");
    b = new Button(shell, SWT.PUSH);
    b.setText("Button 2");
    b = new Button(shell, SWT.PUSH);
    b.setText("Button 3");
    b = new Button(shell, SWT.PUSH);
    b.setText("Not shown");
    b.setVisible(false);
    RowData data = new RowData();
    data.exclude = true;
    b.setLayoutData(data);
    b = new Button(shell, SWT.PUSH);
    b.setText("Button 200 high");
    data = new RowData();
    data.height = 200;
    b.setLayoutData(data);
    b = new Button(shell, SWT.PUSH);
    b.setText("Button 200 wide");
    data = new RowData();
    data.width = 200;
    b.setLayoutData(data);
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





RowLayout: spacing

Specifies the space between controls in pixels



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.List;
import org.eclipse.swt.widgets.Shell;
public class RowLayoutSpacing {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    RowLayout rowLayout = new RowLayout();
    rowLayout.spacing = 40;
    
    shell.setLayout(rowLayout);
    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("button1");
    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(450, 100);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





RowLayout: type

  1. Specifies whether the layout places controls in rows or columns.
  2. The default value is HORIZONTAL.
  3. The other possible value is VERTICAL.



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.List;
import org.eclipse.swt.widgets.Shell;
public class RowLayoutType {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    RowLayout rowLayout = new RowLayout();
    rowLayout.type = SWT.VERTICAL;
    
    shell.setLayout(rowLayout);
    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("button1");
    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(450, 200);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





RowLayout: wrap

Specifies whether controls will be wrapped to the next row or column if there is not enough space left on the current row or column. The default value is true.



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.List;
import org.eclipse.swt.widgets.Shell;
public class RowLayoutWrap {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    RowLayout rowLayout = new RowLayout();
    rowLayout.wrap = false;
    
    shell.setLayout(rowLayout);
    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("button1");
    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(450, 200);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Using all default value from RowLayout

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.List;
import org.eclipse.swt.widgets.Shell;
public class RowLayoutDefaultValue {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    RowLayout rowLayout = new RowLayout();
    shell.setLayout(rowLayout);
    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("button1");
    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(450, 100);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Using RowData Objects

  1. The corresponding layout data for the RowLayout class is the RowData class.
  2. RowData can specify the initial width and height of a control.
  3. Layout data objects should not be reused in SWT/JFace



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 RowLayoutRowData {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    RowLayout rowLayout = new RowLayout();
    shell.setLayout(rowLayout);
    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("button1");
    List list = new List(shell, SWT.BORDER);
    list.add("item 1");
    list.add("item 2");
    list.add("item 3");
    list.setLayoutData(new RowData(100, 35));
    
    Button button2 = new Button(shell, SWT.PUSH);
    button2.setText("button #2");
    shell.setSize(450, 200);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Using RowLayout

  1. places all controls in a single column or row.
  2. it doesn"t force all contained controls to the same size.
  3. wrap controls to a new row or column if it runs out of space.
  4. uses the RowData class to determine initial widths and heights for its controls.



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;
public class RowLayoutTest {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    RowLayout layout = new RowLayout(SWT.VERTICAL);
    layout.marginLeft = 20;
    layout.marginTop = 20;
    layout.justify = true;
    shell.setLayout(layout);
    
    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 b = new Button(shell, SWT.PUSH);
    b.setText("seven");
    b.setLayoutData(new RowData(100, 100));
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}