国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

組織SWT/JFace控件的利器:Layout

2019-11-18 15:27:07
字體:
來源:轉載
供稿:網友

  在可視化編程時代,大多數可視化的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)); 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的效果圖



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 五大连池市| 昌邑市| 德钦县| 铜梁县| 荥经县| 嘉兴市| 封丘县| 河曲县| 德庆县| 资阳市| 新安县| 莱阳市| 锡林郭勒盟| 广南县| 长宁区| 盐亭县| 开原市| 重庆市| 万荣县| 区。| 柳林县| 广汉市| 正定县| 怀远县| 乐陵市| 奉节县| 宣城市| 闽侯县| 连山| 达孜县| 光泽县| 永善县| 曲水县| 汾阳市| 章丘市| 黎平县| 青阳县| 饶阳县| 舟山市| 高平市| 筠连县|