java - java.awt.Desktop.isDesktopSupported 在 javafx 应用程序中返回 false
问题描述
我有这个 javafx 控制器方法试图打开由应用程序生成的本地 html 文件。该函数正在另一个线程中运行。
我使用 fxmlloader 启动应用程序的主类:
@SpringBootApplication
public class DemoAppiumProjectApplication extends Application
{
public static ConfigurableApplicationContext applicationContext;
public static void main(String[] args)
{
applicationContext = SpringApplication.run(DemoAppiumProjectApplication.class, args);
Application.launch(DemoAppiumProjectApplication.class, args);
}
@Override
public void start(Stage stage)
throws Exception
{
stage.setTitle("JS SDK Demo Test Suite");
stage.setScene(new Scene(createRoot()));
stage.show();
}
private Parent createRoot()
throws IOException
{
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(DemoAppiumProjectApplication.class.getResource("/setup.fxml"));
fxmlLoader.setControllerFactory(applicationContext::getBean);
return fxmlLoader.load();
}
}
我的控制器类:
@Override
public void initialize(URL url, ResourceBundle resourceBundle)
{
Task<String> task = new Task<String>()
{
@Override
protected String call()
throws Exception
{
showFinalDialog();
return null;
}
};
new Thread(task).start();
}
public void showFinalDialog()
{
Platform.runLater(() -> {
String text = "Test is complete! Click to view test report.";
Alert alert = new Alert(Alert.AlertType.INFORMATION, text, ButtonType.OK);
alert.getDialogPane().setMinHeight(Region.USE_PREF_SIZE);
Optional<ButtonType> buttonType = alert.showAndWait();
if (!buttonType.isPresent())
{
Platform.exit();
}
else if (buttonType.get() == ButtonType.OK)
{
if (Desktop.isDesktopSupported())
{
try
{
Desktop.getDesktop()
.open(new File(System.getProperty("user.dir") + "/test-output/ExtentReport.html"));
}
catch (IOException e)
{
createAlertWindow(e.getMessage());
}
}
Platform.exit();
}
});
}
现在唯一的问题是条件 Desktop.isDesktopSupported 在此处返回 false。我在另一个项目中写了另一个主函数再次测试,它返回true。
我想这意味着我的操作系统(Windows 10)支持桌面,但我的 javafx 应用程序不支持桌面?
那么我在这里错过了什么?
也许还有另一种推荐的方式来打开这个 html 文件?任何想法都会有所帮助!谢谢!
解决方案
当您已经使用 javafx 时,为什么还要使用桌面打开本地 html 文件。
这是一个例子。
public class HelpWindow extends Application{
@Override
public void start(Stage primaryStage) throws Exception{
FlowPane root = new FlowPane();
primaryStage.setTitle("Help Window");
WebView view = new WebView();
WebEngine engine = view.getEngine();
engine.load( HelpWindow.class.getResource("help.html").toString());
root.getChildren().add(view);
Scene scene = new Scene(root, 790, 675);
primaryStage.setScene(scene);
primaryStage.show();
System.out.println(Desktop.isDesktopSupported());
}
}
这将显示help.html
我作为资源包含的文件。从外观上看,您可能正在生成 .html 文件,因此您必须以不同的方式获取该文件。
<html>
<body>
<p> Here is some text</p>
<a href="https://stackoverflow.com/q/61362829/2067492">original question</a>
</body>
</html>
我还包括打印出Desktop.isDesktopSupported()
对我来说返回 true 的 since 。
我怀疑你遇到的一个问题是Desktop.getDesktop().open()
非阻塞的,所以你的应用程序在你的桌面可以真正打开网页之前就退出了。
推荐阅读
- java - 在输入具有最大和最小日期的情况下,Selenium 测试日期输入有问题
- android - 将检测的 JUnit4 测试与 Target apk 组合成一个 apk
- regex - 在确认空字符串的同时分解字符串中的列表
- c# - .net mvc 角度应用程序中的 CORS 问题
- html - 带按钮的 CSS 过渡,没有跨度覆盖文本
- c# - 程序定义了多个入口点?CS0017 main() 有问题?
- css - Flexbox - 如果有 1 个项目,则居中对齐,如果有 2 个项目,则居中对齐
- azure-devops - 为 Azure DevOps 管道中的每个失败的测试用例创建一个特定的工作项 (Bug)
- c# - 如何平滑路径边缘的速度
- python - 如何对齐列表内的元组内的元素?