如何让 JavaFX 和 Java 11 在 IntelliJ IDEA 中工作 [英] How to get JavaFX and Java 11 working in IntelliJ IDEA

查看:64
本文介绍了如何让 JavaFX 和 Java 11 在 IntelliJ IDEA 中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在最近的 Java 11 版本中,JavaFX 不再包含在 JDK 中.我有一个现有的 JavaFX 项目.我有兴趣学习如何将它从 Java 10 项目更改为 Java 11.我确定我已经正确安装了 Java 11,并且我能够将默认 SDK 从 Java 10 更改为 Java 11.我不知道的是什么知道如何让 JavaFX 与 Java 11 一起工作.

我已在

创建 JavaFX 项目 为项目提供一个名称(如 HelloFX)和一个位置.当项目打开时,无法识别 JavaFX 类.

  1. 设置 JDK 11

转到文件 ->项目结构 ->项目,设置项目SDK为11,也可以设置语言级别为11,设置JDK 11

  1. 创建一个库

转到文件 ->项目结构 ->库并将 JavaFX 11 SDK 作为库添加到项目中.指向 JavaFX SDK 的 lib 文件夹.

应用库后,IDE 将识别 JavaFX 类.

警告:如果你现在运行项目,它会编译但你会得到这个错误:

<块引用>

错误:JavaFX 运行时组件缺失,需要运行此应用程序

显示此错误是因为 Java 11 启动器会检查主类是否扩展了 javafx.application.Application.如果是这种情况,则需要在模块路径上有 javafx.graphics 模块.

  1. 添加虚拟机选项

要解决此问题,请单击运行 ->编辑配置...并添加这些 VM 选项:

<块引用>

--module-path %PATH_TO_FX% --add-modules=javafx.controls,javafx.fxml

请注意,IntelliJ 创建的默认项目使用 FXML,因此需要 javafx.fxml 和 javafx.controls.如果您的项目使用其他模块,您也需要添加它们.单击应用并关闭对话框.

  1. 运行项目

点击运行 ->运行...运行项目,现在应该可以正常工作了.

详细资源: https://www.jetbrains.com/help/idea/javafx.html

With the recent Java 11 release, JavaFX is no longer included in the JDK. I have an existing JavaFX project. I am interested in learning how to change it from a Java 10 project to a Java 11. I am certain I have installed Java 11 correctly, and I am able to change the default SDK from Java 10 to Java 11. What I don't know is how I might get JavaFX working with Java 11.

I have downloaded the latest JavaFX release here. I have extracted it to my Java folder which contains my other JDK's, but I really don't know what to do at this point. I have read some lengthy guides that describe how to connect a project using Maven or Gradle, but I have never used these. Is there a way to add JavaFX support in the IntelliJ menus?

解决方案

Download the appropriate JavaFX SDK for your operating system and unzip it to a desired location, for instance /Users/your-user/Downloads/javafx-sdk-11.

  1. Create a JavaFX project

Create a JavaFX project Provide a name to the project, like HelloFX, and a location. When the project opens, the JavaFX classes are not recognized.

  1. Set JDK 11

Go to File -> Project Structure -> Project, and set the project SDK to 11. You can also set the language level to 11. Set JDK 11

  1. Create a library

Go to File -> Project Structure -> Libraries and add the JavaFX 11 SDK as a library to the project. Point to the lib folder of the JavaFX SDK.

Once the library is applied, the JavaFX classes will be recognized by the IDE.

Warning: If you run now the project it will compile but you will get this error:

Error: JavaFX runtime components are missing, and are required to run this application

This error is shown since the Java 11 launcher checks if the main class extends javafx.application.Application. If that is the case, it is required to have the javafx.graphics module on the module-path.

  1. Add VM options

To solve the issue, click on Run -> Edit Configurations... and add these VM options:

--module-path %PATH_TO_FX% --add-modules=javafx.controls,javafx.fxml

Note that the default project created by IntelliJ uses FXML, so javafx.fxml is required along with javafx.controls. If your project uses other modules, you will need to add them as well. Click apply and close the dialog.

  1. Run the project

Click Run -> Run... to run the project, now it should work fine.

Detailed resource: https://www.jetbrains.com/help/idea/javafx.html

这篇关于如何让 JavaFX 和 Java 11 在 IntelliJ IDEA 中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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