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

在 Java SWT 教程的这一部分中,我们介绍对话框。

对话框窗口或对话框是大多数现代 GUI 应用必不可少的部分。 对话被定义为两个或更多人之间的对话。 在计算机应用中,对话框是一个窗口,用于与应用“对话”。 对话框用于输入数据,修改数据,更改应用设置等。对话框是用户与计算机程序之间进行通信的重要手段。

MessageDialog

MessageBox是一个简单的对话框,向用户提供一些信息。 它可以包含消息,图标和各种按钮。 以可用的MessageBox样式选择图标和按钮。 例如,SWT.ICON_ERROR标志在对话框上放置一个错误图标。

MessageBoxEx.java

  1. package com.zetcode;
  2. import org.eclipse.swt.SWT;
  3. import org.eclipse.swt.layout.RowLayout;
  4. import org.eclipse.swt.widgets.Button;
  5. import org.eclipse.swt.widgets.Display;
  6. import org.eclipse.swt.widgets.MessageBox;
  7. import org.eclipse.swt.widgets.Shell;
  8. /**
  9. * ZetCode Java SWT tutorial
  10. *
  11. * This example shows a simple MessageBox.
  12. *
  13. * Author: Jan Bodnar
  14. * Website: zetcode.com
  15. * Last modified: June 2015
  16. */
  17. public class MessageBoxEx {
  18. private Shell shell;
  19. public MessageBoxEx(Display display) {
  20. initUI(display);
  21. }
  22. private void initUI(Display display) {
  23. shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER);
  24. RowLayout layout = new RowLayout();
  25. layout.marginTop = 50;
  26. layout.marginBottom = 150;
  27. layout.marginLeft = 50;
  28. layout.marginRight = 150;
  29. shell.setLayout(layout);
  30. Button msgBtn = new Button(shell, SWT.PUSH);
  31. msgBtn.setText("Show message");
  32. msgBtn.addListener(SWT.Selection, event -> doShowMessageBox());
  33. shell.setText("Message box");
  34. shell.pack();
  35. shell.open();
  36. while (!shell.isDisposed()) {
  37. if (!display.readAndDispatch()) {
  38. display.sleep();
  39. }
  40. }
  41. }
  42. private void doShowMessageBox() {
  43. int style = SWT.ICON_INFORMATION | SWT.OK;
  44. MessageBox dia = new MessageBox(shell, style);
  45. dia.setText("Information");
  46. dia.setMessage("Download completed.");
  47. dia.open();
  48. }
  49. @SuppressWarnings("unused")
  50. public static void main(String[] args) {
  51. Display display = new Display();
  52. MessageBoxEx ex = new MessageBoxEx(display);
  53. display.dispose();
  54. }
  55. }

窗口上有一个按钮。 单击该按钮将显示一个信息消息对话框。

  1. int style = SWT.ICON_INFORMATION | SWT.OK;

该对话框包含一个信息图标和一个确定按钮。

  1. MessageBox dia = new MessageBox(shell, style);

创建MessageBox的实例,将样式作为第二个参数。

  1. dia.setText("Information");

对话框的标题通过setText()方法设置。

  1. dia.setMessage("Download completed.");

该消息通过setMessage()方法设置。

Java SWT 中的对话框 - 图1

图:MessageBox

请求终止

为了防止数据丢失,以数据为中心的应用在关闭时通常会显示一个确认对话框。 仅在确认对话框后,应用才会终止。

MessageBoxEx2.java

  1. package com.zetcode;
  2. import org.eclipse.swt.SWT;
  3. import org.eclipse.swt.widgets.Display;
  4. import org.eclipse.swt.widgets.Event;
  5. import org.eclipse.swt.widgets.MessageBox;
  6. import org.eclipse.swt.widgets.Shell;
  7. /**
  8. * ZetCode Java SWT tutorial
  9. *
  10. * This example shows a confirmation dialog
  11. * when its closing is initiated.
  12. *
  13. * Author: Jan Bodnar
  14. * Website: zetcode.com
  15. * Last modified: June 2015
  16. */
  17. public class MessageBoxEx2 {
  18. private Shell shell;
  19. public MessageBoxEx2(Display display) {
  20. initUI(display);
  21. }
  22. private void initUI(Display display) {
  23. shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER);
  24. shell.addListener(SWT.Close, event -> doShowMessageBox(event));
  25. shell.setText("Message box");
  26. shell.setSize(350, 300);
  27. shell.open();
  28. while (!shell.isDisposed()) {
  29. if (!display.readAndDispatch()) {
  30. display.sleep();
  31. }
  32. }
  33. }
  34. private void doShowMessageBox(Event event) {
  35. int style = SWT.APPLICATION_MODAL | SWT.ICON_QUESTION | SWT.YES
  36. | SWT.NO;
  37. MessageBox messageBox = new MessageBox(shell, style);
  38. messageBox.setText("Information");
  39. messageBox.setMessage("Really close application?");
  40. event.doit = messageBox.open() == SWT.YES;
  41. }
  42. @SuppressWarnings("unused")
  43. public static void main(String[] args) {
  44. Display display = new Display();
  45. MessageBoxEx2 ex = new MessageBoxEx2(display);
  46. display.dispose();
  47. }
  48. }

此示例显示启动关闭对话框时的确认对话框。

  1. shell.addListener(SWT.Close, event -> doShowMessageBox(event));

我们将监听器挂钩到SWT.Close事件类型。

  1. int style = SWT.APPLICATION_MODAL | SWT.ICON_QUESTION | SWT.YES
  2. | SWT.NO;

确认对话框是模式对话框,包含一个“问题”图标以及“是”和“否”按钮。 (模式对话框将阻止主应用,直到将其关闭。)

  1. event.doit = messageBox.open() == SWT.YES;

doit事件设置为false会取消该事件; 设置为true允许它。

目录对话框

DirectoryDialog是一个对话框,用于选择特定目录的路径。

DirectoryDialogEx.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.DirectoryDialog;
  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 example shows a directory dialog.
  14. *
  15. * Author: Jan Bodnar
  16. * Website: zetcode.com
  17. * Last modified: June 2015
  18. */
  19. public class DirectoryDialogEx {
  20. private Shell shell;
  21. private Label status;
  22. public DirectoryDialogEx(Display display) {
  23. initUI(display);
  24. }
  25. private void initUI(Display display) {
  26. shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER);
  27. status = new Label(shell, SWT.BORDER);
  28. status.setText("Ready");
  29. FormLayout layout = new FormLayout();
  30. shell.setLayout(layout);
  31. FormData labelData = new FormData();
  32. labelData.left = new FormAttachment(0);
  33. labelData.right = new FormAttachment(100);
  34. labelData.bottom = new FormAttachment(100);
  35. status.setLayoutData(labelData);
  36. shell.addListener(SWT.MouseDown, event -> onMouseDown());
  37. shell.setText("DirectoryDialog");
  38. shell.setSize(350, 250);
  39. shell.open();
  40. while (!shell.isDisposed()) {
  41. if (!display.readAndDispatch()) {
  42. display.sleep();
  43. }
  44. }
  45. }
  46. private void onMouseDown() {
  47. DirectoryDialog dialog = new DirectoryDialog(shell);
  48. String path = dialog.open();
  49. if (path != null) {
  50. status.setText(path);
  51. }
  52. }
  53. @SuppressWarnings("unused")
  54. public static void main(String[] args) {
  55. Display display = new Display();
  56. DirectoryDialogEx ex = new DirectoryDialogEx(display);
  57. display.dispose();
  58. }
  59. }

在我们的示例中,我们选择带有DirectoryDialog的目录,并在状态栏中显示其路径。 通过单击窗口区域可以显示该对话框。

  1. shell.addListener(SWT.MouseDown, event -> onMouseDown());

SWT.MouseDown事件的鼠标监听器添加到外壳中。 当我们在窗口上按下鼠标按钮时,将调用onMouseDown()方法。

  1. DirectoryDialog dialog = new DirectoryDialog(shell);

创建了DirectoryDialog

  1. String path = dialog.open();

我们获得所选目录的路径。

  1. if (path != null) {
  2. status.setText(path);
  3. }

如果路径不为空,则在状态标签中显示该路径。

Java SWT 中的对话框 - 图2

图:目录对话框

FontDialog

FontDialog是用于选择字体的对话框。 它通常用于进行一些文本编辑或格式化的应用中。

FontDialogEx.java

  1. package com.zetcode;
  2. import org.eclipse.swt.SWT;
  3. import org.eclipse.swt.graphics.Font;
  4. import org.eclipse.swt.graphics.FontData;
  5. import org.eclipse.swt.layout.RowLayout;
  6. import org.eclipse.swt.widgets.Display;
  7. import org.eclipse.swt.widgets.FontDialog;
  8. import org.eclipse.swt.widgets.Label;
  9. import org.eclipse.swt.widgets.Shell;
  10. /**
  11. * ZetCode Java SWT tutorial
  12. *
  13. * This example shows a font dialog.
  14. *
  15. * Author: Jan Bodnar
  16. * Website: zetcode.com
  17. * Last modified: June 2015
  18. */
  19. public class FontDialogEx {
  20. private Shell shell;
  21. private Label label;
  22. public FontDialogEx(Display display) {
  23. initUI(display);
  24. }
  25. private void initUI(Display display) {
  26. shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER);
  27. RowLayout layout = new RowLayout();
  28. layout.marginHeight = 100;
  29. layout.marginWidth = 100;
  30. shell.setLayout(layout);
  31. label = new Label(shell, SWT.NONE);
  32. label.setText("ZetCode Java SWT tutorial");
  33. shell.addListener(SWT.MouseDown, event -> onMouseDown());
  34. shell.setText("FontDialog");
  35. shell.pack();
  36. shell.open();
  37. while (!shell.isDisposed()) {
  38. if (!display.readAndDispatch()) {
  39. display.sleep();
  40. }
  41. }
  42. }
  43. private void onMouseDown() {
  44. FontDialog dialog = new FontDialog(shell);
  45. FontData fdata = dialog.open();
  46. if (fdata != null) {
  47. Font font = new Font(shell.getDisplay(), fdata);
  48. label.setFont(font);
  49. label.pack();
  50. shell.pack();
  51. font.dispose();
  52. }
  53. }
  54. @SuppressWarnings("unused")
  55. public static void main(String[] args) {
  56. Display display = new Display();
  57. FontDialogEx ex = new FontDialogEx(display);
  58. display.dispose();
  59. }
  60. }

在代码示例中,我们使用FontDialog更改标签的字体。

  1. FontDialog dialog = new FontDialog(shell);

FontDialog已创建。

  1. Font font = new Font(shell.getDisplay(), fdata);

根据字体数据创建Font对象,由字体对话框返回。

  1. label.setFont(font);

字体通过setFont()方法应用于标签。

  1. label.pack();
  2. shell.pack();

pack()方法使标签和外壳适应新的字体类型。

  1. font.dispose();

字体是操作系统资源。 因此,必须在不再需要时将其丢弃。

Java SWT 中的对话框 - 图3

图:FontDialog

ColorDialog

ColorDialog是用于选择颜色的对话框。

ColorDialogEx.java

  1. package com.zetcode;
  2. import org.eclipse.swt.SWT;
  3. import org.eclipse.swt.graphics.Color;
  4. import org.eclipse.swt.graphics.RGB;
  5. import org.eclipse.swt.layout.RowLayout;
  6. import org.eclipse.swt.widgets.ColorDialog;
  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 example shows a color dialog.
  14. *
  15. * Author: Jan Bodnar
  16. * Website: zetcode.com
  17. * Last modified: June 2015
  18. */
  19. public class ColorDialogEx {
  20. private Shell shell;
  21. private Label label;
  22. public ColorDialogEx(Display display) {
  23. initUI(display);
  24. }
  25. private void initUI(Display display) {
  26. shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER);
  27. RowLayout layout = new RowLayout();
  28. layout.marginHeight = 100;
  29. layout.marginWidth = 100;
  30. shell.setLayout(layout);
  31. label = new Label(shell, SWT.NONE);
  32. label.setText("ZetCode Java SWT tutorial");
  33. shell.addListener(SWT.MouseDown, event -> onMouseDown());
  34. shell.setText("ColorDialog");
  35. shell.pack();
  36. shell.open();
  37. while (!shell.isDisposed()) {
  38. if (!display.readAndDispatch()) {
  39. display.sleep();
  40. }
  41. }
  42. }
  43. private void onMouseDown() {
  44. ColorDialog dialog = new ColorDialog(shell);
  45. RGB rgb = dialog.open();
  46. if (rgb != null) {
  47. Color col = new Color(shell.getDisplay(), rgb);
  48. label.setForeground(col);
  49. col.dispose();
  50. }
  51. }
  52. @SuppressWarnings("unused")
  53. public static void main(String[] args) {
  54. Display display = new Display();
  55. ColorDialogEx ex = new ColorDialogEx(display);
  56. display.dispose();
  57. }
  58. }

该示例与上一个示例非常相似。 这次我们更改标签的颜色。 单击窗口区域显示ColorDialog

  1. ColorDialog dialog = new ColorDialog(shell);

我们创建ColorDialog

  1. RGB rgb = dialog.open();

使用ColorDialogopen()方法,我们可以获得 RGB 值。

  1. Color col = new Color(shell.getDisplay(), rgb);
  2. label.setForeground(col);

我们获得颜色值并修改标签的颜色。

  1. col.dispose();

Color是 OS 资源,因此我们在不再需要它时将其处理。

Java SWT 中的对话框 - 图4

图:ColorDialog

文件对话框

FileDialog用于选择文件名。

FileDialogEx.java

  1. package com.zetcode;
  2. import org.eclipse.swt.SWT;
  3. import org.eclipse.swt.layout.RowLayout;
  4. import org.eclipse.swt.widgets.Display;
  5. import org.eclipse.swt.widgets.FileDialog;
  6. import org.eclipse.swt.widgets.Label;
  7. import org.eclipse.swt.widgets.Shell;
  8. /**
  9. * ZetCode Java SWT tutorial
  10. *
  11. * This example shows a file dialog.
  12. *
  13. * Author: Jan Bodnar
  14. * Website: zetcode.com
  15. * Last modified: June 2015
  16. */
  17. public class FileDialogEx {
  18. private Shell shell;
  19. private Label label;
  20. public FileDialogEx(Display display) {
  21. initUI(display);
  22. }
  23. private void initUI(Display display) {
  24. shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER);
  25. RowLayout layout = new RowLayout();
  26. layout.marginHeight = 50;
  27. layout.marginWidth = 50;
  28. shell.setLayout(layout);
  29. label = new Label(shell, SWT.NONE);
  30. String homeDir = System.getProperty("user.home");
  31. label.setText(homeDir);
  32. label.pack();
  33. shell.addListener(SWT.MouseDown, event -> onMouseDown());
  34. shell.setText("FileDialog");
  35. shell.pack();
  36. shell.open();
  37. while (!shell.isDisposed()) {
  38. if (!display.readAndDispatch()) {
  39. display.sleep();
  40. }
  41. }
  42. }
  43. private void onMouseDown() {
  44. FileDialog dialog = new FileDialog(shell, SWT.OPEN);
  45. String[] filterNames = new String[]
  46. {"Java sources", "All Files (*)"};
  47. String[] filterExtensions = new String[]
  48. {"*.java", "*"};
  49. dialog.setFilterNames(filterNames);
  50. dialog.setFilterExtensions(filterExtensions);
  51. String path = dialog.open();
  52. if (path != null) {
  53. label.setText(path);
  54. label.pack();
  55. shell.pack();
  56. }
  57. }
  58. @SuppressWarnings("unused")
  59. public static void main(String[] args) {
  60. Display display = new Display();
  61. FileDialogEx ex = new FileDialogEx(display);
  62. display.dispose();
  63. }
  64. }

该代码示例使用FileDialog选择文件。 该对话框使用过滤器仅显示 Java 源。 所选文件的名称显示在标签中。

  1. label = new Label(shell, SWT.NONE);
  2. String homeDir = System.getProperty("user.home");
  3. label.setText(homeDir);
  4. label.pack();

首先,标签小部件将显示用户的主目录。

  1. FileDialog dialog = new FileDialog(shell, SWT.OPEN);

我们用SWT.OPEN标志创建一个FileDialog。 该对话框可用于打开或保存文件。 对话框的保存行为通过SWT.SAVE常量启用。

  1. String[] filterNames = new String[]
  2. {"Java sources", "All Files (*)"};
  3. String[] filterExtensions = new String[]
  4. {"*.java", "*"};
  5. dialog.setFilterNames(filterNames);
  6. dialog.setFilterExtensions(filterExtensions);

我们使用两个过滤器; 一种用于 Java 源,一种用于所有文件类型。

  1. String path = dialog.open();

使用FileDialogopen()方法检索路径名。

  1. if (path != null) {
  2. label.setText(path);
  3. label.pack();
  4. shell.pack();
  5. }

路径名通过setText()方法设置为标签。 标签和外壳通过pack()方法适应返回路径的大小。

Java SWT 教程的这一部分是关于 SWT 中的对话框的。