当前位置:天才代写 > tutorial > JAVA 教程 > 组织SWT/JFace控件的利器:Layout

组织SWT/JFace控件的利器:Layout

2017-11-12 08:00 星期日 所属: JAVA 教程 浏览:825

副标题#e#

在可视化编程时代,大大都可视化的GUI开拓东西都提供了按必然法则分列Form中的控件的成果。可是对付Java来说,支持可视化开拓的东西并不多,固然有一些这样的东西,但它们大多是第三方的产物,不变性方面有一些欠缺。因此,在许多时候利用Java编写GUI措施时,就要利用机关(Layout)来节制Form上的控件的位置。

本文主要接头如何利用SWT中提供的机关来布置控件的位置,并通过实例来演示这一进程。在SWT中提供了5种机关:FillLayout, RowLayout, GridLayout, FormLayout, and StackLayout。下面我将具体接头这5种机关的利用。

FillLayout

FillLayout是最简朴的机关。它可以将控件横向或纵向举办分列,而且个中每个控件都有同样的宽度或高度。利用FillLayout一般分为2步。

1. 成立一个FillLayout工具。

2. 利用setLayout要领配置Shell工具的机关。

下面代码利用FillLayout在Shell上放了3个按钮,代码如下:

package layout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;
public class TestFillLayout
{
 public static void main(String[] args)
 {
  Display display = new Display();
  Shell shell = new Shell(display, SWT.DIALOG_TRIM);
  shell.setText("FillLayout演示");
  shell.setSize(400, 300);
  // 配置shell的机关
  FillLayout layout = new FillLayout();
  shell.setLayout(layout);
  // 向shell添加控件
  Button button1 = new Button(shell, SWT.PUSH);
  button1.setText("按钮1");
  Button button2 = new Button(shell, SWT.PUSH);
  button2.setText("按钮2");
  Button button3 = new Button(shell, SWT.PUSH);
  button3.setText("按钮3");
  shell.open();
  while (!shell.isDisposed())
  {
   if (!display.readAndDispatch())
   {
    display.sleep();
   }
  }
  display.dispose();
 }
}

界面如图1所示。

组织SWT/JFace控件的利器:Layout

图 1 利用横向FillLayout的Shell界面

假如想要Shell上的控件纵向分列,可以在成立机关时将type属性配置成SWT.VERTICAL。代码如下:

FillLayout layout = new FillLayout();
layout.type = SWT.VERTICAL;
shell.setLayout(layout);

图2是控件纵向分列的结果图

组织SWT/JFace控件的利器:Layout

图 2 利用纵向FillLayout的Shell界面

FillLayout的结构函数重载了2次。个中一个结构函数有一个参数,这个参数就是type。因此,我们也可以通过FillLayout的结构函数对type赋值。

shell.setLayout(new FillLayout(SWT.VERTICAL));


#p#副标题#e#

RowLayout

RowLayout的成果和FillLayout差不多。只是它和FillLayout的最大区别是每个控件并不必然是一样巨细。并且RowLayout是按行分列,这一点和FillLayout是差异的。在一行排满后,就从下一行开始分列。和RowLayout共同利用的尚有一个RowData类。这个类可以配置每一个控件的巨细。下面代码是一个利用RowLayout的小例子。

package layout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;
public class TestRowLayout
{
 public static void main(String[] args)
 {
  Display display = new Display();
  Shell shell = new Shell(display, SWT.DIALOG_TRIM);
  shell.setText("RowLayout演示");
  shell.setSize(220, 200);
  // 将Shell的机关配置成RowLayout
  RowLayout layout = new RowLayout(SWT.HORIZONTAL);
  layout.spacing = 30;
  layout.marginLeft = 30;
  layout.marginTop = 30;
  shell.setLayout(layout);
  RowData rowData = new RowData();
  rowData.height = 50;
  rowData.width = 100;
  // 向shell添加控件
  Button button1 = new Button(shell, SWT.PUSH);
  button1.setText("按钮1");
  button1.setLayoutData(rowData);
  Button button2 = new Button(shell, SWT.PUSH);
  button2.setText("按钮2");
  Button button3 = new Button(shell, SWT.PUSH);
  button3.setText("按钮3");
  shell.open();
  while (!shell.isDisposed())
  {
   if (!display.readAndDispatch())
   {
    display.sleep();
   }
  }
  display.dispose();
 }
}

图3是利用RowLayout的结果图

组织SWT/JFace控件的利器:Layout

图3 RowLayout结果图

#p#分页标题#e#

可以在两个处所配置和RowLayout相关的数据,一个是RowData,它可以配置控件的巨细。别的一个就是RowLayout自己,它可以配置控件开始分列的起点坐标(marginLeft和marginTop)、以及控件之间的间距等信息(spacing)。在RowLayout中有个很重要的属性justify。这个属性默认是false。当它为true时,将自动调解行或整列的控件,使其只管占满一行或一列。

#p#副标题#e#

GridLayout

GridLayout是SWT中最重要的机关。这个机关十分强大,它可以满意大大都的需要。并且它的网络可以通过Composite工具举办嵌套。GridLayout有两个结构函数。一个是默认的结构函数public GridLayout()。别的一个需要两个函数public GridLayout(int numColumns, boolean makeColumnsEqualWidth)。个中numColumns是这个网格的列数,而别的一个参数将使每一列等宽。下面的代码是一个完整的关于GridLayout的例子。

package layout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;
public class TestGridLayout
{
 public static void main(String[] args)
 {
  Display display = new Display();
  Shell shell = new Shell(display);
  shell.setText("GridLayout演示");
  GridLayout layout = new GridLayout();
  layout.numColumns = 3;
  layout.makeColumnsEqualWidth = true;
  shell.setLayout(layout);
  // 成立左上角的按钮
  GridData data = new GridData(GridData.FILL_BOTH);
  data.widthHint = 200; //按钮的初始宽度为200
  Button one = new Button(shell, SWT.PUSH);
  one.setText("按钮1");
  one.setLayoutData(data);
  // 成立一个Composite工具,并在上面放三个按钮
  Composite composite = new Composite(shell, SWT.NONE);
  data = new GridData(GridData.FILL_BOTH);
  data.horizontalSpan = 2; // Composite的占两个Cell
  composite.setLayoutData(data);
  layout = new GridLayout();
  layout.numColumns = 1;
  layout.marginHeight = 15;
  layout.marginRight = 150;
  composite.setLayout(layout);

  // 成立第二个按钮
  data = new GridData(GridData.FILL_BOTH);
  Button two = new Button(composite, SWT.PUSH);
  two.setText("按钮2");
  two.setLayoutData(data);
  // 成立第三个按钮
  data = new GridData(GridData.HORIZONTAL_ALIGN_END);
  Button three = new Button(composite, SWT.PUSH);
  three.setText("按钮3");
  three.setLayoutData(data);
  // 成立第四个按钮
  data = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
  Button four = new Button(composite, SWT.PUSH);
  four.setText("按钮4");
  four.setLayoutData(data);
  // 成立下面的一个长按钮
  data = new GridData();
  data.horizontalAlignment = GridData.FILL;
  data.grabExcessHorizontalSpace = true;
  data.horizontalSpan = 3;
  data.verticalSpan = 2;
  data.heightHint = 150;
  Button five = new Button(shell, SWT.PUSH);
  five.setText("按钮5");
  five.setLayoutData(data);
  shell.pack();
  shell.open();
  while (!shell.isDisposed())
  {
   if (!display.readAndDispatch())
   {
    display.sleep();
   }
  }
  display.dispose();
 }
}

上面的例子将网格设成3列,在第一行的第一列安排了第一个按钮。然后将一个Composite工具放到第一行第二、三列(data.horizontalSpan = 2),在这个工具上放到个按钮。最后在下面成立了第5个按钮。

#p#副标题#e#

FormLayout

FormLayout是SWT中最巨大的机关。在这一部门我将具体接头FormLayout。这个机关固然巨大,但它确可以对控件举办更多的节制。

FormLayout和其它的机关雷同,也有一个FormData类用于配置机关信息。但我其它的机关差异的是FormLayout利用别的一个类来节制控件的巨细和位置,这个类是FormAttachment。控件的上、下、左、右别离要4个FormAttachment工具来节制。通过这些FormAttachment工具可以节制当前控件如安在它的父窗口中显示,以及和其它控件的相对位置。

在FormLayout中,描写位置的4个属性(left、top、right、bottom)都是一个FormAttachment工具。而并不是一个简朴的值。在FormAttachment中较量常用的属性是numerator,这个属性描写控件的4个边距Shell的边沿的间隔。

按百分比安排控件

#p#分页标题#e#

可以通过FormAttachment的numerator属性配置百分比。下面的代码将一个按钮在它的父窗口的宽度的40%处安排按钮。当Shell的尽寸改变后,按钮的位置也随之变革,但按钮的巨细并不改变。

Button button = new Button(shell, SWT.PUSH);
button.setText("按钮");
FormData data = new FormData();
data.left = new FormAttachment(40);
button.setLayoutData(data);

但假如同时配置data的left和right属性又如何呢?假如是这样的话,那么这个按钮的尺寸就会改变。下面的例子将一个按钮放到程度偏向中心的位置。我们可以将left的比例设计为40%,而right的比例配置60%。假如这样配置,无论窗体如何变革,这个按钮始终在程度偏向的中心。这是一个简朴的数学计较。假如一个按钮的左边在40%的位置,而右边在60%的位置,也就是说,按钮的右边距窗体的右边也是40%。这样可以算出,按钮的宽度占窗体的20%(哈哈,要想利用FormLayout,可要有必然的数学基本啊!)。

Button button = new Button(shell, SWT.PUSH);
button.setText("按钮");
FormData data = new FormData();
data.left = new FormAttachment(40);
data.right = new FormAttachment(60);
button.setLayoutData(data);

不外在利用FormAttachment同时给left和right或top和bottom赋值时,不能将这两个比例设成一样的,要不就将按钮压扁了(按钮看不到了,宽或高为0)。假如想要按钮在中心位置,这两个比例的和必需是100。并且left(top)的比例要小于right(bottom)的比例。

配置控件的偏移量

SWT之所以提供了机关,目标就是让你只管少利用绝对坐标来配置控件。但在有时,我们又必需利用绝对坐标。假如是这样的话,你可以利用控件的setLocation来配置,也可以利用FormAttachment提供的偏移量去到达同样的结果。

FormData data = new FormData();
data.left = new FormAttachment(0, 20);
data.top = new FormAttachment(0, 40);
button.setLayoutData(data);

上面的代码相当于setLocation(20, 40),即将按钮配置的位置配置成(20, 40)。

按照其它控件的调解位置

一般在窗体上都有不但一个控件,这些控件之间都是有必然干系的,它们的位置也是相对的。因此,FormLayout为我们提供了按照其它控件配置当控件位置的要领。如按照别的一个控件配置当前控件左边或右边的位置。

下面的例子将按照一个按钮的位置配置别的一个按钮的位置。有两个按钮,第二个按钮按照第一个按钮的底边配置本身的位置。

Button button1 = new Button(shell, SWT.PUSH);
button1.setText("按钮1");
FormData data1 = new FormData();
data1.top = new FormAttachment(20);
data1.left = new FormAttachment(40);
data1.right = new FormAttachment(50);
button1.setLayoutData(data1);
// 第二个按钮
Button button2 = new Button(shell, SWT.PUSH);
button2.setText("按钮2");
FormData data2 = new FormData();
data2.left = new FormAttachment(button1, 0, SWT.CENTER);
data2.top = new FormAttachment(button1);
button2.setLayoutData(data2);

上面的"按钮2"将按照"按钮1"将本身程度偏向的位置配置成"按钮1"的中心位置。

#p#副标题#e#

StackLayout

我们在最后一部门来接头StackLayout,并不是因为StackLayout在这5种机关中最巨大,而是这个机关并不在org.eclipse.swt.layout中,而在org.eclipse.swt.custom中。从这个机关的名子可看出,它是将很多SWT控件放到一个荟萃中,这些控件有同样的尺寸和位置。但只有在最上面的控件才气被显示。

StackLayout只有一个空的结构函数,它只有一个属性topControl,这个属必确定了哪一个控件可以被显示。这个属性默认值是null,也就是说,假如不配置它,那将没有控件被显示在窗体上。但要留意的是,配置topControl并不会将StackLayout中控件删除。

package layout;
import org.eclipse.swt.events.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.SWT;
public class TestStackLayout
{
 public static void main(String[] args)
 {
  Display display = new Display();
  Shell shell = new Shell(display);
  StackLayout layout = new StackLayout();
  shell.setLayout(layout);
  StackLayoutSelectionAdapter adapter = new StackLayoutSelectionAdapter(shell, layout);
  Button one = new Button(shell, SWT.PUSH);
  one.setText("按钮1");
  one.addSelectionListener(adapter);
  Button two = new Button(shell, SWT.PUSH);
  two.setText("按钮2");
  two.addSelectionListener(adapter);
  Button three = new Button(shell, SWT.PUSH);
  three.setText("按钮3");
  three.addSelectionListener(adapter);
  layout.topControl = one;
  shell.open();
  while (!shell.isDisposed())
  {
   if (!display.readAndDispatch())
   {
    display.sleep();
   }
  }
  display.dispose();
 }
}
class StackLayoutSelectionAdapter extends SelectionAdapter
{
 Shell shell;
 StackLayout layout;
 public StackLayoutSelectionAdapter(Shell shell, StackLayout layout)
 {
  this.shell = shell;
  this.layout = layout;
 }
 public void widgetSelected(SelectionEvent event)
 {
  Control control = layout.topControl;
  Control[] children = shell.getChildren();
  int i = 0;
  for (int n = children.length; i < n; i++)
  {
   Control child = children[i];
   if (child == control)
   {
    break;
   }
  }
  ++i;
  if (i >= children.length)
   i = 0;
  layout.topControl = children[i];
  shell.layout();
 }
}

上面这个例子在Shell上有三个按钮,通过按最上面显示的按钮,这三个按钮轮回瓜代显示。

 

    关键字:

[contact-form-7 id="17097" title="form2"]天才代写-代写联系方式