原文: http://zetcode.com/gui/javaswt/layout/

在本章中,我们将展示如何在窗口或对话框中布置窗口小部件。

在设计应用的 GUI 时,我们决定使用哪些小部件以及如何在应用中组织这些小部件。 为了组织小部件,我们使用称为布局容器的专用非可见小部件。

Composite是用于放置子窗口小部件的容器。 Composite的布局管理器是通过setLayout()方法设置的。 Shell也是Composite。 它没有默认的布局管理器,在这种情况下,将使用绝对定位来放置小部件。

SWT 具有以下标准布局类:

  • FillLayout
  • RowLayout
  • FormLayout
  • GridLayout

FillLayout在单个行或列中布置大小相等的小部件。 RowLayout在行或列中布置小部件,并具有填充,环绕和间距选项。 FormLayout通过为小部件的每一侧创建附件来布局小部件。 GridLayout将小部件布置在网格中。

布局类可以具有对应的布局数据类,其中包含特定子项的布局数据。 例如,RowLayout具有名为RowData的布局数据类,GridLayout具有GridData,而FormLayout具有FormData

绝对定位

在大多数情况下,程序员应使用布局管理器。 在某些情况下,我们也可以使用绝对定位。 在绝对定位中,程序员以像素为单位指定每个小部件的位置和大小。 如果我们调整窗口大小,则小部件的大小和位置不会改变。 在各种平台上,应用看起来都不同,在 Linux 上看起来不错,在 Mac OS 上看起来不太正常。 在应用中更改字体可能会破坏布局。 如果我们将应用翻译成另一种语言,则必须重做布局。 对于所有这些问题,仅在有理由的情况下才使用绝对定位,或者您的应用是简单的测试。

绝对定位是通过setSize()setLocation()setBounds()方法完成的。

AbsoluteLayoutEx.java

  1. package com.zetcode;
  2. import org.eclipse.swt.SWT;
  3. import org.eclipse.swt.widgets.Button;
  4. import org.eclipse.swt.widgets.Display;
  5. import org.eclipse.swt.widgets.Shell;
  6. /**
  7. * ZetCode Java SWT tutorial
  8. *
  9. * In this program, we position two
  10. * buttons using absolute coordinates.
  11. *
  12. * Author: Jan Bodnar
  13. * Website: zetcode.com
  14. * Last modified: June 2015
  15. */
  16. public class AbsoluteLayoutEx {
  17. public AbsoluteLayoutEx(Display display) {
  18. initUI(display);
  19. }
  20. private void initUI(Display display) {
  21. Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER);
  22. Button btn1 = new Button(shell, SWT.PUSH);
  23. btn1.setText("Button");
  24. btn1.setBounds(20, 50, 80, 30);
  25. Button btn2 = new Button(shell, SWT.PUSH);
  26. btn2.setText("Button");
  27. btn2.setSize(80, 30);
  28. btn2.setLocation(50, 100);
  29. shell.setText("Absolute layout");
  30. shell.setSize(300, 250);
  31. shell.open();
  32. while (!shell.isDisposed()) {
  33. if (!display.readAndDispatch()) {
  34. display.sleep();
  35. }
  36. }
  37. }
  38. @SuppressWarnings("unused")
  39. public static void main(String[] args) {
  40. Display display = new Display();
  41. AbsoluteLayoutEx ex = new AbsoluteLayoutEx(display);
  42. display.dispose();
  43. }
  44. }

在我们的示例中,我们使用绝对定位在窗口上放置了两个按钮。

  1. btn1.setBounds(20, 50, 80, 30);

setBounds()方法有两件事:将按钮定位在x = 20y = 50,并将按钮的大小设置为width = 80height = 30

  1. button2.setSize(80, 30);
  2. button2.setLocation(50, 100);

在这里,我们分两个步骤进行相同的操作。 首先,我们使用setSize()方法调整按钮的大小。 然后,我们使用setLocation()方法将其定位在窗口上。

Java SWT 中的布局管理 - 图1

图:绝对布局

FillLayout管理器

FillLayout是最简单的布局类。 它将小部件布置在一行或一列中,迫使它们具有相同的大小。

FillLayoutEx.java

  1. package com.zetcode;
  2. import org.eclipse.swt.SWT;
  3. import org.eclipse.swt.graphics.Device;
  4. import org.eclipse.swt.graphics.Image;
  5. import org.eclipse.swt.graphics.Rectangle;
  6. import org.eclipse.swt.layout.FillLayout;
  7. import org.eclipse.swt.widgets.Display;
  8. import org.eclipse.swt.widgets.Label;
  9. import org.eclipse.swt.widgets.Shell;
  10. /**
  11. * ZetCode Java SWT tutorial
  12. *
  13. * This program demonstrates the FillLayout
  14. * manager
  15. *
  16. * Author: Jan Bodnar
  17. * Website: zetcode.com
  18. * Last modified: May 2015
  19. */
  20. public class FillLayoutEx {
  21. private Image castle;
  22. public FillLayoutEx(Display display) {
  23. initUI(display);
  24. }
  25. private void initUI(Display display) {
  26. Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER);
  27. shell.setLayout(new FillLayout());
  28. loadImage(shell);
  29. Label label = new Label(shell, SWT.IMAGE_PNG);
  30. label.setImage(castle);
  31. shell.setText("FillLayout");
  32. Rectangle rect = castle.getBounds();
  33. shell.setSize(rect.width, rect.height);
  34. shell.open();
  35. while (!shell.isDisposed()) {
  36. if (!display.readAndDispatch()) {
  37. display.sleep();
  38. }
  39. }
  40. }
  41. private void loadImage(Shell shell) {
  42. Device dev = shell.getDisplay();
  43. try {
  44. castle = new Image(dev, "redrock.png");
  45. } catch(Exception e) {
  46. System.out.println("Cannot load image");
  47. System.out.println(e.getMessage());
  48. System.exit(1);
  49. }
  50. }
  51. @Override
  52. public void finalize() {
  53. castle.dispose();
  54. }
  55. public static void main(String[] args) {
  56. Display display = new Display();
  57. FillLayoutEx app = new FillLayoutEx(display);
  58. app.finalize();
  59. display.dispose();
  60. }
  61. }

在我们的示例中,我们使用此管理器显示图像。

  1. shell.setLayout(new FillLayout());

我们将FillLayout设置为外壳的布局类。 使用setLayout()方法设置布局。

  1. Rectangle rect = castle.getBounds();
  2. shell.setSize(rect.width, rect.height);

我们找出图片的大小来调整外壳的大小,以完全适合图像的大小。

  1. Label label = new Label(shell, SWT.IMAGE_PNG);
  2. label.setImage(castle);

我们将图像设置为标签小部件。

  1. private void loadImage(Shell shell) {
  2. Device dev = shell.getDisplay();
  3. try {
  4. castle = new Image(dev, "redrock.png");
  5. } catch(Exception e) {
  6. System.out.println("Cannot load image");
  7. System.out.println(e.getMessage());
  8. System.exit(1);
  9. }
  10. }

loadImage()方法从磁盘加载图像。

Java SWT 中的布局管理 - 图2

图:FillLayout

RowLayout

RowLayout管理器将所有小部件放置在一行或一列中。

RowLayoutEx.java

  1. package com.zetcode;
  2. import org.eclipse.swt.SWT;
  3. import org.eclipse.swt.layout.RowData;
  4. import org.eclipse.swt.layout.RowLayout;
  5. import org.eclipse.swt.widgets.Button;
  6. import org.eclipse.swt.widgets.Display;
  7. import org.eclipse.swt.widgets.Shell;
  8. /**
  9. * ZetCode Java SWT tutorial
  10. *
  11. * This program demonstrates the RowLayout
  12. * manager.
  13. *
  14. * Author: Jan Bodnar
  15. * Website: zetcode.com
  16. * Last modified: June 2015
  17. */
  18. public class RowLayoutEx {
  19. public RowLayoutEx(Display display) {
  20. initUI(display);
  21. }
  22. private void initUI(Display display) {
  23. Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER);
  24. RowLayout rowLayout = new RowLayout(SWT.HORIZONTAL);
  25. rowLayout.marginTop = 10;
  26. rowLayout.marginBottom = 10;
  27. rowLayout.marginLeft = 5;
  28. rowLayout.marginRight = 5;
  29. rowLayout.spacing = 10;
  30. shell.setLayout(rowLayout);
  31. Button btn1 = new Button(shell, SWT.PUSH);
  32. btn1.setText("Button");
  33. btn1.setLayoutData(new RowData(80, 30));
  34. Button btn2 = new Button(shell, SWT.PUSH);
  35. btn2.setText("Button");
  36. btn2.setLayoutData(new RowData(80, 30));
  37. Button btn3 = new Button(shell, SWT.PUSH);
  38. btn3.setText("Button");
  39. btn3.setLayoutData(new RowData(80, 30));
  40. shell.setText("RowLayout");
  41. shell.pack();
  42. shell.open();
  43. while (!shell.isDisposed()) {
  44. if (!display.readAndDispatch()) {
  45. display.sleep();
  46. }
  47. }
  48. }
  49. @SuppressWarnings("unused")
  50. public static void main(String[] args) {
  51. Display display = new Display();
  52. RowLayoutEx ex = new RowLayoutEx(display);
  53. display.dispose();
  54. }
  55. }

在我们的示例中,我们创建了三个按钮的行。

  1. RowLayout rowLayout = new RowLayout(SWT.HORIZONTAL);

将创建水平RowLayout。 这些小部件将放置在一行中。

  1. rowLayout.marginTop = 10;
  2. rowLayout.marginBottom = 10;
  3. rowLayout.marginLeft = 5;
  4. rowLayout.marginRight = 5;

边距指定沿容器边缘的空间。

  1. rowLayout.spacing = 10;

spacing属性指定按钮之间的间距。

  1. shell.setLayout(rowLayout);

我们将行布局指定为外壳布局。

  1. Button btn1 = new Button(shell, SWT.PUSH);
  2. btn1.setText("Button");
  3. btn1.setLayoutData(new RowData(80, 30));

创建了ButtonsetLayoutData()指定按钮的大小。

Java SWT 中的布局管理 - 图3

图:RowLayout管理器

按钮

在最后一个示例中,我们使用FormLayout管理器创建一个示例。 该管理器使用两个对象FormDataFormAttachment控制子项的位置和大小。

ButtonsEx.java

  1. package com.zetcode;
  2. import org.eclipse.swt.SWT;
  3. import org.eclipse.swt.layout.FormAttachment;
  4. import org.eclipse.swt.layout.FormData;
  5. import org.eclipse.swt.layout.FormLayout;
  6. import org.eclipse.swt.widgets.Button;
  7. import org.eclipse.swt.widgets.Display;
  8. import org.eclipse.swt.widgets.Shell;
  9. /**
  10. * ZetCode Java SWT tutorial
  11. *
  12. * In this program, we position two buttons
  13. * in the bottom right corner of the window.
  14. *
  15. * Author: Jan Bodnar
  16. * Website: zetcode.com
  17. * Last modified: May 2015
  18. */
  19. public class ButtonsEx {
  20. public ButtonsEx(Display display) {
  21. initUI(display);
  22. }
  23. private void initUI(Display display) {
  24. Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER);
  25. FormLayout layout = new FormLayout();
  26. shell.setLayout(layout);
  27. Button okBtn = new Button(shell, SWT.PUSH);
  28. okBtn.setText("OK");
  29. Button cancBtn = new Button(shell, SWT.PUSH);
  30. cancBtn.setText("Cancel");
  31. FormData cancelData = new FormData(80, 30);
  32. cancelData.right = new FormAttachment(98);
  33. cancelData.bottom = new FormAttachment(95);
  34. cancBtn.setLayoutData(cancelData);
  35. FormData okData = new FormData(80, 30);
  36. okData.right = new FormAttachment(cancBtn, -5, SWT.LEFT);
  37. okData.bottom = new FormAttachment(cancBtn, 0, SWT.BOTTOM);
  38. okBtn.setLayoutData(okData);
  39. shell.setText("Buttons");
  40. shell.setSize(350, 200);
  41. shell.open();
  42. while (!shell.isDisposed()) {
  43. if (!display.readAndDispatch()) {
  44. display.sleep();
  45. }
  46. }
  47. }
  48. @SuppressWarnings("unused")
  49. public static void main(String[] args) {
  50. Display display = new Display();
  51. ButtonsEx ex = new ButtonsEx(display);
  52. display.dispose();
  53. }
  54. }

在此代码示例中,我们在窗口的右下角放置了两个按钮。

  1. FormLayout layout = new FormLayout();
  2. shell.setLayout(layout);

FormLayout管理器已创建。

  1. Button okBtn = new Button(shell, SWT.PUSH);
  2. okBtn.setText("OK");
  3. Button cancBtn = new Button(shell, SWT.PUSH);
  4. cancBtn.setText("Cancel");

创建两个按钮并将其设置到外壳。

  1. FormData cancelData = new FormData(80, 30);

取消按钮的大小为80x30

  1. cancelData.right = new FormAttachment(98);
  2. cancelData.bottom = new FormAttachment(95);

按钮的右侧附着在窗口宽度的 98% 处。 按钮的底部固定在窗口高度的 95% 处。

  1. okData.right = new FormAttachment(cancelButton, -5, SWT.LEFT);
  2. okData.bottom = new FormAttachment(cancelButton, 0, SWT.BOTTOM);

“确定”按钮的右侧位于“取消”按钮的左侧 5 像素处。 “确定”按钮的底部与“取消”按钮的底部对齐。

Java SWT 中的布局管理 - 图4

图:按钮

新建文件夹

在下面的示例中,我们使用FormLayoutRowLayout管理器创建窗口布局。

NewFolderEx.java

  1. package com.zetcode;
  2. import org.eclipse.swt.SWT;
  3. import org.eclipse.swt.layout.FormAttachment;
  4. import org.eclipse.swt.layout.FormData;
  5. import org.eclipse.swt.layout.FormLayout;
  6. import org.eclipse.swt.layout.RowData;
  7. import org.eclipse.swt.layout.RowLayout;
  8. import org.eclipse.swt.widgets.Button;
  9. import org.eclipse.swt.widgets.Composite;
  10. import org.eclipse.swt.widgets.Display;
  11. import org.eclipse.swt.widgets.Label;
  12. import org.eclipse.swt.widgets.Shell;
  13. import org.eclipse.swt.widgets.Text;
  14. /**
  15. * ZetCode Java SWT tutorial
  16. *
  17. * This program creates a layout using a
  18. * FormLayout and a RowLayout.
  19. *
  20. * Author: Jan Bodnar
  21. * Website: zetcode.com
  22. * Last modified: June 2015
  23. */
  24. public class NewFolderEx {
  25. public NewFolderEx(Display display) {
  26. initUI(display);
  27. }
  28. private void initUI(Display display) {
  29. Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER);
  30. shell.setLayout(new FormLayout());
  31. Label lbl = new Label(shell, SWT.LEFT);
  32. lbl.setText("Name:");
  33. FormData data1 = new FormData();
  34. data1.left = new FormAttachment(0, 5);
  35. data1.top = new FormAttachment(0, 10);
  36. lbl.setLayoutData(data1);
  37. Text text = new Text(shell, SWT.SINGLE);
  38. FormData data2 = new FormData();
  39. data2.left = new FormAttachment(lbl, 15);
  40. data2.top = new FormAttachment(0, 10);
  41. data2.right = new FormAttachment(100, -5);
  42. text.setLayoutData(data2);
  43. Composite com = new Composite(shell, SWT.NONE);
  44. RowLayout rowLayout = new RowLayout();
  45. com.setLayout(rowLayout);
  46. Button okBtn = new Button(com, SWT.PUSH);
  47. okBtn.setText("OK");
  48. okBtn.setLayoutData(new RowData(80, 30));
  49. Button closeBtn = new Button(com, SWT.PUSH);
  50. closeBtn.setText("Close");
  51. closeBtn.setLayoutData(new RowData(80, 30));
  52. FormData data3 = new FormData();
  53. data3.bottom = new FormAttachment(100, -5);
  54. data3.right = new FormAttachment(100, 0);
  55. com.setLayoutData(data3);
  56. Text mainText = new Text(shell, SWT.MULTI | SWT.BORDER);
  57. FormData data4 = new FormData();
  58. data4.width = 250;
  59. data4.height = 180;
  60. data4.top = new FormAttachment(text, 10);
  61. data4.left = new FormAttachment(0, 5);
  62. data4.right = new FormAttachment(100, -5);
  63. data4.bottom = new FormAttachment(com, -10);
  64. mainText.setLayoutData(data4);
  65. shell.setText("New folder");
  66. shell.pack();
  67. shell.open();
  68. while (!shell.isDisposed()) {
  69. if (!display.readAndDispatch())
  70. display.sleep();
  71. }
  72. }
  73. @SuppressWarnings("unused")
  74. public static void main(String[] args) {
  75. Display display = new Display();
  76. NewFolderEx ex = new NewFolderEx(display);
  77. display.dispose();
  78. }
  79. }

在示例中,有标签,文本和按钮小部件。

  1. Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER);
  2. shell.setLayout(new FormLayout());

FormLayout设置为外壳的主布局管理器。

  1. Label lbl = new Label(shell, SWT.LEFT);
  2. lbl.setText("Name:");
  3. FormData data1 = new FormData();
  4. data1.left = new FormAttachment(0, 5);
  5. data1.top = new FormAttachment(0, 10);
  6. lbl.setLayoutData(data1);

标签窗口小部件附在窗口的左上角。

  1. Text text = new Text(shell, SWT.SINGLE);
  2. FormData data2 = new FormData();
  3. data2.left = new FormAttachment(lbl, 15);
  4. data2.top = new FormAttachment(0, 10);
  5. data2.right = new FormAttachment(100, -5);
  6. text.setLayoutData(data2);

在标签旁边,我们放置一个Text控件。 文本控件的左侧相对于标签放置。

  1. Composite com = new Composite(shell, SWT.NONE);
  2. RowLayout rowLayout = new RowLayout();
  3. com.setLayout(rowLayout);

创建一个Composite并将其设置为RowLayout管理器。 这两个按钮进入该容器。 将RowLayout用于按钮要比直接通过FormLayout进行组织要容易一些。

  1. Button okBtn = new Button(com, SWT.PUSH);
  2. okBtn.setText("OK");
  3. okBtn.setLayoutData(new RowData(80, 30));
  4. Button closeBtn = new Button(com, SWT.PUSH);
  5. closeBtn.setText("Close");
  6. closeBtn.setLayoutData(new RowData(80, 30));

创建两个按钮。 他们的父部件是Composite

  1. FormData data3 = new FormData();
  2. data3.bottom = new FormAttachment(100, -5);
  3. data3.right = new FormAttachment(100, 0);
  4. com.setLayoutData(data3);

Composite本身与FormLayout一起放置在窗口的底部。 负值是与相邻小部件或窗口边界的偏移量。

  1. Text mainText = new Text(shell, SWT.MULTI | SWT.BORDER);
  2. FormData data4 = new FormData();
  3. data4.width = 250;
  4. data4.height = 180;
  5. data4.top = new FormAttachment(text, 10);
  6. data4.left = new FormAttachment(0, 5);
  7. data4.right = new FormAttachment(100, -5);
  8. data4.bottom = new FormAttachment(com, -10);
  9. mainText.setLayoutData(data4);

最后,创建了Text主窗口小部件。 它占用了大部分窗口区域。 widthheight属性指定控件的初始首选大小。

Java SWT 中的布局管理 - 图5

图:新文件夹

GridLayout

GridLayout管理器将其子窗口小部件放入网格中。

GridLayoutEx.java

  1. package com.zetcode;
  2. import org.eclipse.swt.SWT;
  3. import org.eclipse.swt.graphics.Color;
  4. import org.eclipse.swt.layout.GridData;
  5. import org.eclipse.swt.layout.GridLayout;
  6. import org.eclipse.swt.widgets.Display;
  7. import org.eclipse.swt.widgets.Label;
  8. import org.eclipse.swt.widgets.Shell;
  9. /**
  10. * ZetCode Java SWT tutorial
  11. *
  12. * This example presents the GridLayout.
  13. *
  14. * Author: Jan Bodnar
  15. * Website: zetcode.com
  16. * Last modified: June 2015
  17. */
  18. public class GridLayoutEx {
  19. public GridLayoutEx(Display display) {
  20. initUI(display);
  21. }
  22. private void initUI(Display display) {
  23. Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER);
  24. Color col = new Color(display, 100, 200, 100);
  25. shell.setBackground(col);
  26. col.dispose();
  27. GridLayout layout = new GridLayout(2, false);
  28. shell.setLayout(layout);
  29. Label lbl1 = new Label(shell, SWT.NONE);
  30. GridData gd1 = new GridData(SWT.FILL, SWT.FILL, true, true);
  31. lbl1.setLayoutData(gd1);
  32. Color col1 = new Color(display, 250, 155, 100);
  33. lbl1.setBackground(col1);
  34. col1.dispose();
  35. Label lbl2 = new Label(shell, SWT.NONE);
  36. GridData gd2 = new GridData(SWT.FILL, SWT.FILL, true, true);
  37. gd2.heightHint = 100;
  38. lbl2.setLayoutData(gd2);
  39. Color col2 = new Color(display, 10, 155, 100);
  40. lbl2.setBackground(col2);
  41. col2.dispose();
  42. Label lbl3 = new Label(shell, SWT.NONE);
  43. GridData gd3 = new GridData(SWT.FILL, SWT.FILL, true, true);
  44. gd3.widthHint = 300;
  45. gd3.heightHint = 100;
  46. gd3.horizontalSpan = 2;
  47. lbl3.setLayoutData(gd3);
  48. Color col3 = new Color(display, 100, 205, 200);
  49. lbl3.setBackground(col3);
  50. col3.dispose();
  51. shell.setText("Grid");
  52. shell.pack();
  53. shell.open();
  54. while (!shell.isDisposed()) {
  55. if (!display.readAndDispatch())
  56. display.sleep();
  57. }
  58. }
  59. @SuppressWarnings("unused")
  60. public static void main(String[] args) {
  61. Display display = new Display();
  62. GridLayoutEx ex = new GridLayoutEx(display);
  63. display.dispose();
  64. }
  65. }

在示例中,我们在网格中放置了三个标签。 每个标签具有不同的背景色。

  1. Color col = new Color(display, 100, 200, 100);
  2. shell.setBackground(col);
  3. col.dispose();

setBackground()方法为外壳设置背景色。

  1. GridLayout layout = new GridLayout(2, false);
  2. shell.setLayout(layout);

实例化GridLayout管理器并将其设置为外壳的布局管理器。 网格由 2 列组成。

  1. Label lbl1 = new Label(shell, SWT.NONE);
  2. GridData gd1 = new GridData(SWT.FILL, SWT.FILL, true, true);
  3. lbl1.setLayoutData(gd1);

第一个标签进入网格的左上角单元格。 GridData类的四个参数使标签组件填充其单元格并在两个方向上扩展。

  1. Label lbl2 = new Label(shell, SWT.NONE);
  2. GridData gd2 = new GridData(SWT.FILL, SWT.FILL, true, true);
  3. gd2.heightHint = 100;
  4. lbl2.setLayoutData(gd2);

第二个标签转到相邻的单元格。 heightHint属性指定标签的首选高度。 请注意,它也会影响先前的窗口小部件,因为该属性有效地设置了行的首选高度。

  1. Label lbl3 = new Label(shell, SWT.NONE);
  2. GridData gd3 = new GridData(SWT.FILL, SWT.FILL, true, true);
  3. gd3.widthHint = 300;
  4. gd3.heightHint = 100;
  5. gd3.horizontalSpan = 2;
  6. lbl3.setLayoutData(gd3);

第三个标签进入第二行。 horizontalSpan属性使标签跨越两列。

Java SWT 中的布局管理 - 图6

图:简单 GridLayout

计算器

在下面的示例中,我们使用GridLayout管理器创建计算器的框架。

CalculatorEx.java

  1. package com.zetcode;
  2. import org.eclipse.swt.SWT;
  3. import org.eclipse.swt.layout.GridData;
  4. import org.eclipse.swt.layout.GridLayout;
  5. import org.eclipse.swt.widgets.Button;
  6. import org.eclipse.swt.widgets.Display;
  7. import org.eclipse.swt.widgets.Label;
  8. import org.eclipse.swt.widgets.Shell;
  9. import org.eclipse.swt.widgets.Text;
  10. /**
  11. * ZetCode Java SWT tutorial
  12. *
  13. * In this program, we use the GridLayout to
  14. * create a calculator skeleton.
  15. *
  16. * Author: Jan Bodnar
  17. * Website: zetcode.com
  18. * Last modified: June 2015
  19. */
  20. public class CalculatorEx {
  21. public CalculatorEx(Display display) {
  22. initUI(display);
  23. }
  24. private void initUI(Display display) {
  25. Shell shell = new Shell(display, SWT.DIALOG_TRIM | SWT.CENTER);
  26. GridLayout gl = new GridLayout(4, true);
  27. gl.marginHeight = 5;
  28. shell.setLayout(gl);
  29. String[] buttons = {
  30. "Cls", "Bck", "", "Close", "7", "8", "9", "/", "4",
  31. "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+"
  32. };
  33. Text text = new Text(shell, SWT.SINGLE);
  34. GridData gridData = new GridData();
  35. gridData.horizontalSpan = 4;
  36. gridData.horizontalAlignment = GridData.FILL;
  37. text.setLayoutData(gridData);
  38. for (int i = 0; i < buttons.length; i++) {
  39. if (i == 2) {
  40. Label lbl = new Label(shell, SWT.CENTER);
  41. GridData gd = new GridData(SWT.FILL, SWT.FILL, false, false);
  42. lbl.setLayoutData(gd);
  43. } else {
  44. Button btn = new Button(shell, SWT.PUSH);
  45. btn.setText(buttons[i]);
  46. GridData gd = new GridData(SWT.FILL, SWT.FILL, false, false);
  47. gd.widthHint = 50;
  48. gd.heightHint = 30;
  49. btn.setLayoutData(gd);
  50. }
  51. }
  52. shell.setText("Calculator");
  53. shell.pack();
  54. shell.open();
  55. while (!shell.isDisposed()) {
  56. if (!display.readAndDispatch()) {
  57. display.sleep();
  58. }
  59. }
  60. }
  61. @SuppressWarnings("unused")
  62. public static void main(String[] args) {
  63. Display display = new Display();
  64. CalculatorEx ex = new CalculatorEx(display);
  65. display.dispose();
  66. }
  67. }

我们使用GridLayout管理器创建计算器的框架。 我们使用三种类型的小部件:文本小部件,标签小部件和几个按钮。

  1. Shell shell = new Shell(display, SWT.DIALOG_TRIM | SWT.CENTER);

使用SWT.DIALOG_TRIM标志,使窗口不可调整大小。

  1. GridLayout gl = new GridLayout(4, true);
  2. gl.marginHeight = 5;
  3. shell.setLayout(gl);

我们创建一个具有 4 列的GridLayout,并提供顶部和底部页边距。

  1. Text text = new Text(shell, SWT.SINGLE);
  2. GridData gridData = new GridData();
  3. gridData.horizontalSpan = 4;
  4. gridData.horizontalAlignment = GridData.FILL;
  5. text.setLayoutData(gridData);

GridData是与GridLayout关联的布局数据对象。 使用horizontalSpan属性,我们使文本小部件跨越所有四列。 设置为GridData.FILLhorizontalAlignment使文本窗口小部件填充布局管理器分配给它的整个区域。

  1. Button btn = new Button(shell, SWT.PUSH);
  2. btn.setText(buttons[i]);
  3. GridData gd = new GridData(SWT.FILL, SWT.FILL, false, false);
  4. gd.widthHint = 50;
  5. gd.heightHint = 30;
  6. btn.setLayoutData(gd);

for循环内,我们创建按钮并将其放入网格中。 通过widthHintheightHint属性,我们可以设置按钮的首选大小。

Java SWT 中的布局管理 - 图7

图:计算机骨架

在 Java SWT 教程的这一部分中,我们讨论了小部件的布局管理。