如何在 Eclipse 中使用 JavaFX 11?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52013505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:18:49  来源:igfitidea点击:

How do I use JavaFX 11 in Eclipse?

javaeclipsejavafxjavafx-11

提问by Alex Chashin

I have some trouble with 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. 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 openjfxand I can't make Eclipse work with JavaFX.

我在使用 JavaFX 时遇到了一些麻烦。我想开始创建应用程序,桌面或移动,至少是一些东西。所以我发现我可以使用 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?

我不确定不将 JDK 8 与包含的 JavaFX 一起使用是否有任何意义,但无论如何,我如何在 Eclipse 中的这种情况下使用 JavaFX?

采纳答案by ItachiUchiha

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 JDKstarting JDK 11. I stress on Oracle JDK because JavaFX was never a part of OpenJDK. Not even in OpenJDK 8.

从 JDK 11 开始,JavaFX 将与Oracle JDK分离。我强调 Oracle JDK,因为 JavaFX 从来不是 OpenJDK 的一部分。甚至在 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 Photonplugin.

对于 Eclipse 中的 Java 11 支持,您需要安装Java 11 Support for 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 openjfxand I can't make eclipse work with JavaFX.

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?

我使用安装了 openjfx sudo apt install openjfx,但无法使 Eclipse 与 JavaFX 一起工作。

我不确定不将 JDK 8 与包含的 JavaFX 一起使用是否有任何意义,但无论如何,我如何在 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 hereor hereand 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.9M3to 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.javarequired)
  • 基于模块的项目
  • 非基于模块的项目(不需要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 项目并将 JavaFX jars 从 Java FX 11 SDK 添加到项目的模块路径。

enter image description here

在此处输入图片说明

Create a module.infoand declare its dependency of javafx.controlsmodule. javafx11is the name of the package which contains your Java file.

创建一个module.info并声明它对javafx.controls模块的依赖。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 eitherthe 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 参数添加到项目的运行配置中:

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

Run the program \o/

运行程序\o/

回答by Basil Bourque

tl;dr

tl;博士

To most easily get started with JavaFX, use the Oracle-branded release of Java 8 where JavaFX 8is bundled and easily available.

要最轻松地开始使用 JavaFX,请使用 Oracle 品牌的 Java 8 版本,其中捆绑了JavaFX 8且易于获取。

For technical details, see Using JavaFX in JRE 8. Look to the Linkedand Relatedsections of the web page for many related postings.

有关技术详细信息,请参阅在 JRE 8 中使用 JavaFX。查看网页的链接相关部分以获取许多相关帖子。

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. No single app ever uses all of it.

以前,Java SE(标准版)是一大块软件,随着越来越多的添加而不断增长。没有一个应用程序会使用所有这些。

A decision was taken to break Java SE into many separate chunks to be defined formally as “modules”. 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. See the jlinktool.

决定将 Java SE 分成许多单独的块,正式定义为“模块”。一个主要的好处是,应用程序可能与 Java SE 运行时捆绑在一起,该运行时仅由实际需要的模块组成,省略了未使用的模块。查看jlink工具。

As a byproduct of this modularization, some older and less-popular parts such as CORBAare 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). Similarly, some Java EErelated modules will be removed from Java SE and turned over to the Jakarta EEproject, logically a more appropriate home. See JEP 320: Remove the Java EE and CORBA Modules.

作为这种模块化的副产品,一些较旧且不太流行的部分(例如CORBA)正在被丢弃,不再作为 Java 的标准部分提供(尽管其他方面决定可以选择)。同样,一些Java EE相关模块将从 Java SE 中删除,并移交给Jakarta EE项目,逻辑上更合适的家。请参阅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. See:

Java 11 中采取的这些步骤之一是停止将 JavaFX 与 Java SE 捆绑在一起。看:

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. 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 8 完成,现在开始使用 JavaFX 开发将是最简单的。JavaFX 库与 Java 8 捆绑在一起。而且您无需学习模块化,也无需与 IDE(例如 Eclipse)和项目设置搏斗识别模块。如果您没有迫切需要使用最新版本的 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.javafile.
  • Updating your IDE (Eclipse, etc.) and other tools to later versions supporting both modularization and Java 11.
  • Configuring modules in your build tools, such as Maven or Gradle
  • Configuring modules in your IDE, such as Eclipse
  • Downloading JavaFX modules, or using a dependency manager such as Maven to do so
  • Java 模块化一般,包括module-info.java文件。
  • 将您的 IDE(Eclipse 等)和其他工具更新到支持模块化和 Java 11 的更高版本。
  • 在构建工具中配置模块,例如 Maven 或 Gradle
  • 在 IDE 中配置模块,例如 Eclipse
  • 下载 JavaFX 模块,或使用依赖管理器(例如 Maven)来执行此操作

Those points are too much to cover here, and have been covered in many other Questions on Stack Overflow. Besides, Java 11has not yet been formally released.

这些要点太多了,无法在此处涵盖,并且在 Stack Overflow 上的许多其他问题中已涵盖。此外,Java 11尚未正式发布。

Perhaps this article will help, How to Create a Project With JavaFX on JDK 11.

也许这篇文章会有所帮助,How to Create a Project With JavaFX on JDK 11

To learn much more about Java modularization, read the blogand the book, The Java Module System, by Nicolai Parlog.

要了解有关 Java 模块化的更多信息,请阅读Nicolai Parlog的博客和书籍The Java Module System

回答by Joseph Schmidt

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. Neat!

如果您没有使用 Eclipse 和/或只是想学习(或者是一个没有帮助的教授/助教的学生),BlueJ 目前已经内置了 JavaFX 并且可以使用,因此不需要额外的设置或下载。整洁的!