如何在IntelliJ IDEA中使用JavaFX和Java 11 [英] How to get JavaFX and Java 11 working in IntelliJ IDEA

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

问题描述

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

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.

我已经在此处下载了最新的JavaFX版本.我已经将它解压缩到包含其他JDK的Java文件夹中,但是我现在真的不知道该怎么办.我已经阅读了一些冗长的指南,这些指南描述了如何使用Maven或Gradle连接项目,但是我从未使用过.有没有办法在IntelliJ菜单中添加JavaFX支持?

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?

推荐答案

为您的操作系统下载适当的JavaFX SDK,并将其解压缩到所需的位置,例如/Users/your-user/Downloads/javafx-sdk-11

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. 创建一个JavaFX项目

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

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. 设置JDK 11

转到文件"->项目结构"->项目",然后将项目SDK设置为11.您还可以将语言级别设置为11.设置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. 创建一个库

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

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.

一旦应用了库,JavaFX类将被IDE识别.

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:

错误:缺少JavaFX运行时组件,它们是运行所必需的 此应用程序

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

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

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. 添加VM选项

要解决此问题,请单击运行"->编辑配置...",然后添加以下VM选项:

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

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

请注意,IntelliJ创建的默认项目使用FXML,因此javafx.fxml与javafx.controls一起是必需的.如果您的项目使用其他模块,则还需要添加它们. 单击应用",然后关闭对话框.

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. 运行项目

单击运行"->运行..."以运行该项目,现在它应该可以正常运行.

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

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

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