在IDEA中将SceneBuilder与JavaFX 11结合使用 [英] Use SceneBuilder with JavaFX 11 in IDEA

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

问题描述

我正在使用IDEA(与OpenJDK 11配合使用),并且试图使用SceneBuilder来显示我的FXML文件.它可以工作,除非当我使用继承的JavaFX组件时,例如:

I am using IDEA (with OpenJDK 11), and I am trying to use the SceneBuilder to display my FXML files. It works, except when I use inherited JavaFX components, for example :

public class MyLabel extends Label {
    public MyLabel(){
        super();
        setText("42");
    }
}

我总是出错:

java.lang.UnsupportedClassVersionError: sample/MyLabel has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0

因此,我查看了我的IDEA安装文件夹(C:\ Users \ me \ AppData \ Local \ JetBrains \ Toolbox \ apps \ IDEA-C \ ch-0 \ xxx.xxxx.xxx \ plugins \ javaFX \ lib),并且我看到IDEA正在使用SceneBuilder的Java 8版本("SceneBuilderKit-8.2.0.jar").

So I looked in my IDEA installation folder (C:\Users\me\AppData\Local\JetBrains\Toolbox\apps\IDEA-C\ch-0\xxx.xxxx.xxx\plugins\javaFX\lib), and I saw that IDEA is using the version for Java 8 of the SceneBuilder ("SceneBuilderKit-8.2.0.jar").

我想知道如何在IDEA中使用 Java 11版本.我已经尝试在参数中填充SceneBuilder路径,但是没有用.

I am wondering how I can use the Java 11 version in IDEA. I already tried to fill the SceneBuilder path in the parameters, but it didn't work.

推荐答案

如果IntelliJ的嵌入式Scene Builder版本是8.2,那么您不能指望它可以渲染"其类路径中为Java版本大于8(52级).

If IntelliJ's embedded Scene Builder version is 8.2, then you can't expect that it can "render" any class in its classpath compiled for a Java version greater than 8 (level 52).

嵌入式Scene Builder只是运行的另一个Java应用程序,基本上是一个运行在IntelliJ编辑器顶部的Swing组件中的JavaFX应用程序.

The embedded Scene Builder is just another Java application running, basically a JavaFX application running embedded in a Swing component on top of a IntelliJ editor.

Java 8

为了处理使用内置控件的常规FXML文件,Scene Builder使用JDK将jfxrt.jar加载到类路径.

In order to work with regular FXML files that use built-in controls, Scene Builder uses the JDK to load the jfxrt.jar to the classpath.

如果您使用Java 8创建自己的自定义控件(此问题有一个很好的示例说明如何执行此操作),并且您不编译该项目,当您尝试使用Scene Builder时,您会得到一个ClassNotFoundException,因为该控件是不在类路径中,而Scene Builder不知道如何处理它.

If you create your own custom control using Java 8 (this question has a good sample on how to do this), and you don't compile the project, when you try Scene Builder you will get a ClassNotFoundException, because the control is not in the classpath, and Scene Builder doesn't know how to deal with it.

但是,在构建项目之后,结果类也将添加到类路径中,并且您可以看到呈现的控件.

But after you build the project, the resulting classes are added to the classpath as well, and you can see the control rendered.

Java 11

当您使用Java 11创建自定义控件时,就不再有jfxrt.jar.

When you create the custom control with Java 11, as you know, there is no more jfxrt.jar.

按照入门 docs 的要求,您需要添加JavaFX 11 SDK 和VM选项

Following the Getting Started docs, you need to add the JavaFX 11 SDK to your project, and the VM options

--module-path ${PATH_TO_FX} --add-modules=javafx.controls,javafx.fxml

以运行项目.

如果您有一个带有内置控件的 regular FXML文件,并转到嵌入式Scene Builder,它将加载并呈现该文件.这是为什么?因为它可能使用的是IntelliJ附带的内部JDK 1.8,所以FXML文件(包含纯xml,不是Java文件!)可以由(Java 8)FXMLLoader解析并呈现.

If you have a regular FXML file, with just built-in controls, and go to the embedded Scene Builder, it will load and render it. Why is that? Because it is probably using the internal JDK 1.8 shipped with IntelliJ, so the FXML file (containing pure xml, not a java file!) can be parsed by the (Java 8) FXMLLoader and rendered.

但是,如果您尝试使用自定义控件,则在构建项目后,您将得到提到的异常:

But if you try your custom control, after building the project, you will get the exception you mentioned:

因为您正在使用Java 11进行编译,所以现在的级别是55.

since you are compiling with Java 11, and the level now is 55.

有什么办法可以使它工作?如前所述,您需要做的就是将控件加载到类路径中,但级别为52.

Is there any way to make it work? As mentioned before, all you need to do is load your control into the classpath, but with 52 level.

因此,您需要做的就是将项目的目标级别设置为1.8,在项目结构"->项目"->项目语言级别"中

So all you need to do is set the target level of your project to 1.8, in Project Structure -> Project -> Project Language level

现在再次构建您的项目,您将能够使用嵌入式Scene Builder查看自定义控件:

Now build again your project, and you will be able to see the custom control with the embedded Scene Builder:

由于您的项目现在与Java 8兼容,因此可以验证您将能够运行:

Since your project is now compatible with Java 8, you can verify that you will be able to run:

/path-to-java-8/java -jar out/production/Custom_jar/Custom.jar

无论如何,如果您不希望达到该级别,那么当然可以保留Java 11并使用独立的

In any case, if you don't want that level, you can of course keep Java 11 and use the stand-alone Scene Builder 11.

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

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