简体   繁体   English

如何在Eclipse中使用JavaFX 11?

[英]How do I use JavaFX 11 in Eclipse?

I have some trouble with JavaFX. 我在JavaFX上遇到了一些麻烦。 I wanted to start creating apps, desktop or mobile, at least something. 我想开始创建应用程序,桌面或移动,至少是一些东西。 So I found out I could use the JavaFX library for it. 所以我发现我可以使用JavaFX库。 But as far as I understood, it was excluded from JDK 9. I'm actually using OpenJDK 11 on Ubuntu 18 (though Eclipse writes I have the JavaSE 10 environment, that is where I'm also a bit confused) and I installed OpenJFX using sudo apt install openjfx and I can't make Eclipse work with JavaFX. 但据我所知,它被排除在JDK 9之外。我实际上是在Ubuntu 18上使用OpenJDK 11(虽然Eclipse写道我有JavaSE 10环境,这也是我有点困惑的地方)我安装了OpenJFX使用sudo apt install openjfx ,我无法使Eclipse与JavaFX一起工作。

I'm not sure if there's any sense not to use JDK 8 with the included JavaFX, but anyway, how can I use JavaFX in such conditions in Eclipse? 我不确定是否有任何意义不使用包含JavaFX的JDK 8,但无论如何,如何在Eclipse中使用JavaFX?

There are multiple points in your post which needs clarification. 您的帖子中有多个要点需要澄清。 I will try to answer them in different bullet points: 我将尝试在不同的要点中回答它们:

But as far as I understood, it(JavaFX) was excluded from JDK 9. 但据我所知,它(JavaFX)被排除在JDK 9之外。

JavaFX will be decoupled from Oracle JDK starting JDK 11. I stress on Oracle JDK because JavaFX was never a part of OpenJDK. JavaFX将从JDK 11开始与Oracle JDK分离。我强调Oracle JDK,因为JavaFX从未成为OpenJDK的一部分。 Not even in OpenJDK 8. 甚至在OpenJDK 8中也没有。

I'm actually using OpenJDK 11 on Ubuntu 18 (Though eclipse writes I have JavaSE 10 environment, that is where I'm also a bit confused) 我实际上在Ubuntu 18上使用OpenJDK 11(虽然eclipse写道我有JavaSE 10环境,这也是我有点困惑的地方)

For Java 11 support in Eclipse, you need to install Java 11 Support for Eclipse Photon plugin. 对于Eclipse中的Java 11支持,您需要安装Java 11支持Eclipse Photon插件。

Here are a few Examples on how to run Java 11 applications in Eclipse 以下是有关如何在Eclipse中运行Java 11应用程序的一些示例

I installed openjfx using sudo apt install openjfx and I can't make eclipse work with JavaFX. 我使用sudo apt install openjfx ,我无法使用JavaFX进行eclipse工作。

I'm not sure if there's any sense not to use JDK 8 with included JavaFX, but anyway, how can I use JavaFX in such conditions in eclipse? 我不确定是否有任何意义不使用包含JavaFX的JDK 8,但无论如何,我如何在eclipse中使用JavaFX?

Since OpenJDK 11 or Oracle JDK 11 will not come bundled with JavaFX, your best bet is to either download the JavaFX SDK from here or here and load them in your IDE. 由于OpenJDK 11或Oracle JDK 11不会与JavaFX捆绑在一起,因此最好从此处此处下载JavaFX SDK并将其加载到IDE中。

If you are used to build tools, you can directly use the JavaFX runtime jars which are available in Maven Central . 如果您习惯于构建工具,则可以直接使用Maven Central中提供JavaFX运行时jar

For a tutorial on how to run JavaFX 11 on OpenJDK 11, you can follow: 有关如何在OpenJDK 11上运行JavaFX 11的教程,您可以按照:

JavaFX 11 and Eclipse JavaFX 11和Eclipse

At the time of writing this post, you need Eclipse 4.9M3 to work with JavaFX 11. 在撰写本文时,您需要Eclipse 4.9M3才能使用JavaFX 11。

Once you have eclipse, JDK 11 and JavaFX 11 SDK, you can either opt to create: 一旦你有了eclipse,JDK 11和JavaFX 11 SDK,你可以选择创建:

  • Module based project 基于模块的项目
  • Non-module based project (No module-info.java required) 非基于模块的项目(无需module-info.java

Module based Project 基于模块的项目

Create a Java project and add JavaFX jars from the Java FX 11 SDK to the module path of the project. 创建Java项目并将Java FX 11 SDK中的JavaFX jar添加到项目的模块路径中。

在此输入图像描述

Create a module.info and declare its dependency of javafx.controls module. 创建module.info并声明其对javafx.controls模块的依赖关系。 javafx11 is the name of the package which contains your Java file. javafx11是包含Java文件的包的名称。

module javafx11 {
    requires javafx.controls;
    exports javafx11;
}

Run the program \\o/ 运行程序\\ o /

Non-module based Project 非基于模块的项目

Create a Java project and add JavaFX jars from the Java FX 11 SDK to either the module-path or classpath of the project. 创建一个Java项目,并从Java FX 11 SDK添加JavaFX的罐子无论是模块路径或项目的类路径。

Add the following JVM args to the run configuration of the project: 将以下JVM args添加到项目的运行配置中:

--module-path=path-to-javafx-skd/lib --add-modules=javafx.controls

Run the program \\o/ 运行程序\\ o /

tl;dr TL;博士

To most easily get started with JavaFX, use the Oracle-branded release of Java 8 where JavaFX 8 is bundled and easily available. 要最容易使用JavaFX,请使用Java版本的Java 8, JavaFX 8捆绑在一起并且易于使用。

For technical details, see Using JavaFX in JRE 8 . 有关技术详细信息,请参阅在JRE 8中使用JavaFX Look to the Linked and Related sections of the web page for many related postings. 查看网页的链接相关部分,了解许多相关的帖子。

Java Modularization Java模块化

The Java platform is in the process of a sweeping reformulation, known as modularization . Java平台正在进行彻底的重组,称为模块化

Previously, Java SE (standard edition) was one big monolith of software, ever-growing with more and more being added. 以前, Java SE (标准版)是软件的一大块,不断增长,越来越多的软件被添加。 No single app ever uses all of it. 没有一个应用程序曾经使用过它。

A decision was taken to break Java SE into many separate chunks to be defined formally as “modules”. 我们决定将Java SE分解为许多单独的块,以便正式定义为“模块”。 One major benefit is that an app may be bundled with a Java SE runtime composed of only the modules actually needed, with unused modules omitted. 一个主要的好处是应用程序可以与仅由实际需要的模块组成的Java SE运行时捆绑在一起,省略未使用的模块。 See the jlink tool. 请参阅jlink工具。

As a byproduct of this modularization, some older and less-popular parts such as CORBA are being dropped, to no longer be carried as a standard part of Java (though offered for other parties to pick up if they so decide). 作为这种模块化的副产品,一些较老的和不太受欢迎的部分如CORBA正在被丢弃,不再作为Java的标准部分携带(尽管如果他们这样决定就提供给其他方提供)。 Similarly, some Java EE related modules will be removed from Java SE and turned over to the Jakarta EE project, logically a more appropriate home. 类似地,一些Java EE相关模块将从Java SE中删除并转交给Jakarta EE项目,逻辑上是一个更合适的家庭。 See JEP 320: Remove the Java EE and CORBA Modules . 请参阅JEP 320:删除Java EE和CORBA模块

The process of modularization and reorganization is a years-long ongoing effort. 模块化和重组的过程是一项长达数年的持续努力。 Much was done in Java 9 and Java 10. Some of the final steps are being done in Java 11. 在Java 9和Java 10中已经完成了很多工作。最后一些步骤是在Java 11中完成的。

One of these steps being taken in Java 11 is to cease bundling JavaFX with Java SE. Java 11中采用的其中一个步骤是停止将JavaFX与Java SE捆绑在一起。 See: 看到:

So getting started with JavaFX development right now will be easiest if done with Java 8. The JavaFX libraries are bundled in with Java 8. And you need not learn about modularization, nor need to wrestle your IDE (such as Eclipse) and project settings to recognize modules. 因此,如果使用Java 8,现在开始使用JavaFX开发将是最简单的.JavaFX库与Java 8捆绑在一起。您无需了解模块化,也无需将IDE(例如Eclipse)和项目设置与识别模块。 If you do not have a pressing need to use the very last versions of Java or JavaFX, stick with 8 until the modularization process and tools gets smoothed out, likely next year 2019. 如果您没有迫切需要使用Java或JavaFX的最新版本,请坚持使用8,直到模块化过程和工具得到平滑,可能是明年2019年。

If you insist on using Java 11, you need to learn about: 如果您坚持使用Java 11,则需要了解:

  • Java modularization in general, including the module-info.java file. 一般是Java模块化,包括module-info.java文件。
  • Updating your IDE (Eclipse, etc.) and other tools to later versions supporting both modularization and Java 11. 将IDE(Eclipse等)和其他工具更新到支持模块化和Java 11的更高版本。
  • Configuring modules in your build tools, such as Maven or Gradle 在构建工具中配置模块,例如Maven或Gradle
  • Configuring modules in your IDE, such as Eclipse 在IDE中配置模块,例如Eclipse
  • Downloading JavaFX modules, or using a dependency manager such as Maven to do so 下载JavaFX模块,或使用Maven等依赖管理器来执行此操作

Those points are too much to cover here, and have been covered in many other Questions on Stack Overflow. 这些要点太多了,无法在这里讨论,并且已经在Stack Overflow的许多其他问题中有所涉及。 Besides, Java 11 has not yet been formally released. 此外, Java 11尚未正式发布。

Perhaps this article will help, How to Create a Project With JavaFX on JDK 11 . 也许这篇文章将有助于, 如何在JDK 11上使用JavaFX创建项目

To learn much more about Java modularization, read the blog and the book, The Java Module System , by Nicolai Parlog. 要了解有关Java模块化的更多信息,请阅读Nicolai Parlog撰写的博客和书籍Java Module System

I've had to struggle through this on about 20 computers now, so I made the following checklist: 我现在不得不在大约20台电脑上挣扎,所以我做了以下清单:

[ ] download javafx11 from javafx11's website, put on desktop
[ ] create a MODULE based project
[ ] right click project, go to BUILD PATH
[ ] add the downloaded javafx.base/control/graphics as external jar files
[ ] put the files in a package (eg: my_big_package)
[ ] put the following in the module.java file:
module javafx11 {
    requires javafx.controls;
    exports my_big_package;
}
[ ] eat a donut from the break room

If you're not married to Eclipse and/or just trying to learn (or are a student with an unhelpful professor/TAs), BlueJ currently has JavaFX already built into it and ready to go, so no extra setup or download is necessary. 如果你没有与Eclipse结婚和/或只是想学习(或者是一个没有帮助的教授/助教的学生),BlueJ目前已经内置了JavaFX并准备好了,所以不需要额外的设置或下载。 Neat! 整齐!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM