如何与桌面类集成

原文: https://docs.oracle.com/javase/tutorial/uiswing/misc/desktop.html

Java™标准版第 6 版缩小了本机应用程序和 Java 应用程序的性能和集成之间的差距。除了新的系统托盘功能,启动画面支持以及 JTables 的增强打印外,Java SE 版本 6 还提供了 Desktop API(java.awt.Desktop)API ,允许 Java 应用程序与主机平台上与特定文件类型关联的默认应用程序进行交互。

新功能由 Desktop 类提供。 API 源自 JDesktop Integration Components(JDIC)项目。 JDIC 项目的目标是使桌面的“基于 Java 技术的应用程序成为一等公民”,实现无缝集成。 JDIC 为 Java 应用程序提供对本机桌面提供的功能和工具的访问。关于新的 Desktop API,这意味着 Java 应用程序可以执行以下操作:

  • 使用特定的统一资源标识符(URI)启动主机系统的默认浏览器
  • 启动主机系统的默认电子邮件客户端
  • 启动应用程序以打开,编辑或打印与这些应用程序关联的文件

Note:

Desktop API 使用主机操作系统的文件关联来启动与特定文件类型关联的应用程序。例如,如果 OpenDocument 文本(.odt)文件扩展名与 OpenOffice Writer 应用程序相关联,则 Java 应用程序可以启动 OpenOffice Writer 以打开,编辑甚至打印具有该关联的文件。根据主机系统,不同的应用程序可能与不同的操作相关联。例如,如果无法打印特定文件,请首先检查其扩展名是否在给定操作系统上具有打印关联。


使用 isDesktopSupported() 方法确定 Desktop API 是否可用。在 Solaris 操作系统和 Linux 平台上,此 API 依赖于 Gnome 库。如果这些库不可用,则此方法将返回 false。在确定支持 Desktop API 之后,即isDesktopSupported()返回 true,应用程序可以使用静态方法 getDesktop() 检索Desktop实例。

如果应用程序在没有键盘,鼠标或监视器(“无头”环境)的环境中运行,getDesktop()方法将抛出java.awt.HeadlessException

检索后,Desktop实例允许应用程序浏览,邮寄,打开,编辑甚至打印文件或 URI,但前提是检索到的Desktop实例支持这些活动。这些活动中的每一个都称为操作,每个活动都表示为Desktop.Action枚举实例:

  • BROWSE - 表示主机默认浏览器执行的浏览操作。
  • MAIL - 表示主机的默认电子邮件客户端执行的邮件操作。
  • OPEN - 表示与打开特定文件类型相关联的应用程序执行的打开操作。
  • EDIT - 表示与编辑特定文件类型相关联的应用程序执行的编辑操作
  • PRINT - 表示与打印特定文件类型相关联的应用程序执行的打印操作。

即使在相同的文件类型上,也可以针对这些不同的动作注册不同的应用。例如,可以为 OPEN 操作启动 Firefox 浏览器,为 EDIT 操作启动 Emacs,为 PRINT 操作启动不同的应用程序。主机桌面的关联用于确定应调用哪个应用程序。使用 JDK 6 中当前版本的 Desktop API 无法操作桌面文件关联,并且此时只能使用与平台相关的工具创建或更改这些关联。

以下示例显示了上述功能。


Try this:

  1. 编译并运行该示例,请参考示例索引
  2. 将出现 DesktopDemo 对话框。

    DesktopDemo application.

  3. 在 URI 文本字段中输入 URI 值,例如 - https://docs.oracle.com/javase/tutorial

  4. 按“启动浏览器”按钮。
  5. 确保打开默认浏览器窗口并加载 Tutorial 主页面。
  6. 将 URI 更改为任意值,按“启动浏览器”按钮,并确保成功加载您请求的网页。
  7. 切换回 DesktopDemo 对话框,并在电子邮件文本字段中输入邮件收件人名称。您也可以使用支持 CC,BCC,SUBJECT 和 BODY 字段的mailto方案,例如 - duke@example.com?SUBJECT=Hello Duke!
  8. 按“启动邮件”按钮。
  9. 将出现默认电子邮件客户端的合成对话框。确保“收件人”和“主题”字段如下所示。

    DesktopDemo application.

  10. 您可以继续撰写邮件或尝试在电子邮件字段中输入邮件架构的不同组合。

  11. 切换回 DesktopDemo 对话框,然后按省略号按钮选择任何文本文件。
  12. 选择“打开”,“编辑”或“打印”以设置操作类型,然后按“启动应用程序”按钮。
  13. 确保操作正确完成。尝试其他文件格式,例如.odt.html.pdf。注意:如果您尝试编辑.pdf文件,DesktopDemo 将返回以下消息:Cannot perform the given operation to the <file name> file

以下代码片段提供了有关 DeskDemo 应用程序实现的更多详细信息。 DesktopDemo 构造器在实例化 UI 后立即禁用少数组件,并检查 Desktop API 是否可用。

  1. public DesktopDemo() {
  2. // init all gui components
  3. initComponents();
  4. // disable buttons that launch browser, email client,
  5. // disable buttons that open, edit, print files
  6. disableActions();
  7. // before any Desktop APIs are used, first check whether the API is
  8. // supported by this particular VM on this particular host
  9. if (Desktop.isDesktopSupported()) {
  10. desktop = Desktop.getDesktop();
  11. // now enable buttons for actions that are supported.
  12. enableSupportedActions();
  13. }
  14. ...
  15. /**
  16. * Disable all graphical components until we know
  17. * whether their functionality is supported.
  18. */
  19. private void disableActions() {
  20. txtBrowserURI.setEnabled(false);
  21. btnLaunchBrowser.setEnabled(false);
  22. txtMailTo.setEnabled(false);
  23. btnLaunchEmail.setEnabled(false);
  24. rbEdit.setEnabled(false);
  25. rbOpen.setEnabled(false);
  26. rbPrint.setEnabled(false);
  27. txtFile.setEnabled(false);
  28. btnLaunchApplication.setEnabled(false);
  29. }
  30. ...

获取 Desktop 对象后,您可以查询对象以找出支持的特定操作。如果 Desktop 对象不支持特定操作,或者 Desktop API 本身不受支持,则 DesktopDemo 只会禁用受影响的图形组件。

  1. /**
  2. * Enable actions that are supported on this host.
  3. * The actions are the following: open browser,
  4. * open email client, and open, edit, and print
  5. * files using their associated application.
  6. */
  7. private void enableSupportedActions() {
  8. if (desktop.isSupported(Desktop.Action.BROWSE)) {
  9. txtBrowserURI.setEnabled(true);
  10. btnLaunchBrowser.setEnabled(true);
  11. }
  12. if (desktop.isSupported(Desktop.Action.MAIL)) {
  13. txtMailTo.setEnabled(true);
  14. btnLaunchEmail.setEnabled(true);
  15. }
  16. if (desktop.isSupported(Desktop.Action.OPEN)) {
  17. rbOpen.setEnabled(true);
  18. }
  19. if (desktop.isSupported(Desktop.Action.EDIT)) {
  20. rbEdit.setEnabled(true);
  21. }
  22. if (desktop.isSupported(Desktop.Action.PRINT)) {
  23. rbPrint.setEnabled(true);
  24. }
  25. if (rbEdit.isEnabled() || rbOpen.isEnabled() || rbPrint.isEnabled()) {
  26. txtFile.setEnabled(true);
  27. btnLaunchApplication.setEnabled(true);
  28. }
  29. }

browse(uri)方法可以抛出各种异常,包括如果 URI 为 null 则为 NullPointerException;如果不支持 BROWSE 操作,则为 UnsupportedOperationException。如果无法找到或启动默认浏览器或应用程序,则此方法可能抛出 IOException;如果安全管理器拒绝调用,则抛出 SecurityException。

  1. private void onLaunchBrowser(ActionEvent evt) {
  2. URI uri = null;
  3. try {
  4. uri = new URI(txtBrowserURI.getText());
  5. desktop.browse(uri);
  6. } catch(IOException ioe) {
  7. System.out.println("The system cannot find the " + uri +
  8. " file specified");
  9. //ioe.printStackTrace();
  10. } catch(URISyntaxException use) {
  11. System.out.println("Illegal character in path");
  12. //use.printStackTrace();
  13. }
  14. }

如果支持该操作,应用程序可以通过调用此 Desktop 实例的mail(uriMailTo)方法来启动主机的默认电子邮件客户端。

  1. private void onLaunchMail(ActionEvent evt) {
  2. String mailTo = txtMailTo.getText();
  3. URI uriMailTo = null;
  4. try {
  5. if (mailTo.length() > 0) {
  6. uriMailTo = new URI("mailto", mailTo, null);
  7. desktop.mail(uriMailTo);
  8. } else {
  9. desktop.mail();
  10. }
  11. } catch(IOException ioe) {
  12. ioe.printStackTrace();
  13. } catch(URISyntaxException use) {
  14. use.printStackTrace();
  15. }
  16. }

Java 应用程序可以分别使用Desktop类的open()edit()print()方法打开,编辑和打印其关联应用程序中的文件。

  1. private void onLaunchDefaultApplication(ActionEvent evt) {
  2. String fileName = txtFile.getText();
  3. File file = new File(fileName);
  4. try {
  5. switch(action) {
  6. case OPEN:
  7. desktop.open(file);
  8. break;
  9. case EDIT:
  10. desktop.edit(file);
  11. break;
  12. case PRINT:
  13. desktop.print(file);
  14. break;
  15. }
  16. } catch (IOException ioe) {
  17. //ioe.printStackTrace();
  18. System.out.println("Cannot perform the given operation
  19. to the " + file + " file");
  20. }
  21. }

该演示的完整代码可在 DesktopDemo.java 文件中找到。

Desktop类允许 Java 应用程序启动处理 URI 或文件的本机桌面应用程序。

方法 目的
isDesktopSupported() 测试当前平台是否支持此类。如果支持,请使用getDesktop()检索实例。
getDesktop() 返回当前浏览器上下文的Desktop实例。在某些平台上,可能不支持 Desktop API。使用isDesktopSupported()方法确定是否支持当前桌面。
isSupported(Desktop.Action) 测试当前平台是否支持某个操作。使用Desktop.Action枚举的以下常数:BROWSEEDITMAILOPENPRINT
浏览(URI) 启动默认浏览器以显示 URI。如果默认浏览器无法处理指定的 URI,则调用为处理指定类型的 URI 而注册的应用程序。应用程序由 URI 的协议和路径确定,由URI类定义。
邮件(URI) 启动用户默认邮件客户端的邮件撰写窗口,填写mailto: URI指定的邮件字段。
打开(文件) 启动关联的应用程序以打开文件。
编辑(文件) 启动关联的编辑器应用程序并打开文件进行编辑。
打印(文件) 使用关联应用程序的 print 命令,使用本机桌面打印工具打印文件。

使用 Desktop API 的示例

下表列出了使用 Desktop 类集成的示例。

在哪里描述 笔记
DesktopDemo 这个部分 使用指定的 URI 和默认电子邮件客户端启动主机系统的默认浏览器;启动应用程序以打开,编辑或打印文件。

« PreviousTrailNext »