从JavaFX打开外部应用程序 [英] Open External Application From JavaFX

查看:266
本文介绍了从JavaFX打开外部应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了一种使用HostServices在默认浏览器中打开链接的方法.

I found a way to open a link on default browser using HostServices.

getHostServices().showDocument("http://www.google.com");

  • 有什么方法可以在默认媒体播放器中打开媒体吗?
  • 是否可以启动特定的文件应用程序?
    • Is there any way to open a media in default media player?
    • Is there any way to launch a specific File or Application?
    • 推荐答案

      如果要在浏览器中打开具有http:方案的URL,或者使用该文件类型的默认应用程序打开文件,您引用的HostServices.showDocument(...)方法提供了一种纯JavaFX"方法.请注意,就我所知,您不能使用它来从Web服务器下载文件并使用默认应用程序将其打开.

      If you want to either open a URL which has an http: scheme in the browser, or open a file using the default application for that file type, the HostServices.showDocument(...) method you referenced provides a "pure JavaFX" way to do this. Note that you can't use this (as far as I can tell) to download a file from a web server and open it with the default application.

      要使用默认应用程序打开文件,必须将文件转换为file: URL的字符串表示形式.这是一个简单的示例:

      To open a file with the default application, you must convert the file to the string representation of the file: URL. Here is a simple example:

      import java.io.File;
      
      import javafx.application.Application;
      import javafx.event.ActionEvent;
      import javafx.event.EventHandler;
      import javafx.geometry.Insets;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.control.Label;
      import javafx.scene.control.TextField;
      import javafx.scene.layout.HBox;
      import javafx.scene.layout.VBox;
      import javafx.stage.FileChooser;
      import javafx.stage.Stage;
      
      public class OpenResourceNatively extends Application {
      
          @Override
          public void start(Stage primaryStage) {
              TextField textField = new TextField("http://stackoverflow.com/questions/39898704");
              Button openURLButton = new Button("Open URL");
              EventHandler<ActionEvent> handler = e -> open(textField.getText());
              textField.setOnAction(handler);
              openURLButton.setOnAction(handler);
      
              FileChooser fileChooser = new FileChooser();
              Button openFileButton = new Button("Open File...");
              openFileButton.setOnAction(e -> {
                  File file = fileChooser.showOpenDialog(primaryStage);
                  if (file != null) {
                      open(file.toURI().toString());
                  }
              });
      
              VBox root = new VBox(5, 
                      new HBox(new Label("URL:"), textField, openURLButton),
                      new HBox(openFileButton)
              );
      
              root.setPadding(new Insets(20));
              primaryStage.setScene(new Scene(root));
              primaryStage.show();
          }
      
          private void open(String resource) {
              getHostServices().showDocument(resource);
          }
      
          public static void main(String[] args) {
              launch(args);
          }
      }
      

      这篇关于从JavaFX打开外部应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆