简体   繁体   English

在场景构建器中打开自定义控件时出现UnsupportedClassVersionError

[英]UnsupportedClassVersionError when opening custom controls in scene builder

My custom class: 我的自定义类:

public class IntegerField extends TextField {
    public IntegerField() {
        super();
        restrictToNumbersOnly();
    }

    public IntegerField(String text) {
        super(text);
        restrictToNumbersOnly();
    }
    public void restrictToNumbersOnly() {
         // Irrelevant
    }
}

I then try to add this custom class like this: 然后,我尝试添加此自定义类,如下所示:

<IntegerField fx:id="example" />

IntelliJ's Scene Builder outputs this error: IntelliJ的场景生成器输出以下错误:

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

I've tried searching for this error but haven't found anything that explains why this shouldn't work. 我尝试搜索此错误,但未找到任何能解释为什么此方法不起作用的信息。 The examples I've found have indicated that it should be possible to just extend from the control class, and import it directly into the fxml file, and it does. 我发现的示例表明,应该可以从控件类进行扩展,然后将其直接导入到fxml文件中,并且确实可以。 It works perfectly when running the program, it's scene builder that is the issue. 它在运行程序时完美运行,这是场景构建器。

I realize that this is a Java version issue, but the whole project is set to Java 9 (53) and I've experimented with changing the versions to no avail. 我意识到这是Java版本的问题,但是整个项目都设置为Java 9(53),并且我已经尝试过将版本更改为无效。

You've compiled your class with Java 9's target level (thus generating a class version of 53), but you're trying to run it with Java 8. 您已经使用Java 9的目标级别编译了类(从而生成了53的类版本),但是您尝试使用Java 8来运行它。

Either run it with Java 9, or build it with a target language level of 8. 可以使用Java 9运行它,也可以使用目标语言级别8来构建它。

I have not found a fix for the in-built Scene Builder, but I have found a workaround on an external Scene Builder, using these steps: 我没有找到内置Scene Builder的修复程序,但是使用以下步骤找到了外部Scene Builder的解决方法:

  • Install Gluon Scene Builder. 安装Gluon Scene Builder。
  • Moving NumberField to it's own package. 将NumberField移到它自己的包中。
  • Making a custom fxml containing only a NumberField. 制作仅包含NumberField的自定义fxml。
  • Adding JavaFX Application to the package. 将JavaFX Application添加到软件包中。
  • Define the package as an artifact. 将包定义为工件。
  • Build artifact. 构建工件。
  • Adding custom JAR in Gluon Scene Builder. 在Gluon Scene Builder中添加自定义JAR。

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

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