简体   繁体   English

JavaFX 使用label时出现异常 class

[英]JavaFX exception when using label class

The classes I have used so far in the codeblock all work.到目前为止,我在代码块中使用的类都有效。 As soon as I try to use Label, even if I dont add it to the Scene I get the following errors.一旦我尝试使用 Label,即使我没有将它添加到场景中,我也会收到以下错误。 I use Intellij, I was thinking that the classes I use are already available in Intellij and Label is not one of the included, and that's why I get the error.我使用 Intellij,我在想我使用的类在 Intellij 中已经可用,而 Label 不是其中之一,这就是我收到错误的原因。

I have downloaded JavaFX 13 and the folder is on my desktop, in the Project settings - Libraries the path for JavaFX goes to Desktop/javafx-sdk-13.0.2/lib我已经下载了 JavaFX 13 并且文件夹在我的桌面上,在项目设置 - 库中 JavaFX 的路径转到 Desktop/javafx-sdk-13.0.2/lib

Code代码

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class CompoundInterest extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
//        Group root = new Group();
        GridPane pane = new GridPane();
        pane.setAlignment(Pos.CENTER);
        pane.setPadding(new Insets(5,5,5,5));
        pane.setHgap(5.5);
        pane.setVgap(5.5);

        Label label = new Label();
        Text text1 = new Text("Test");
        pane.add(text1,1,1);
//        pane.add(new Label("Username"), 0, 0);

        Scene scene = new Scene(pane, 400, 400);
        primaryStage.setResizable(false);
        primaryStage.setTitle("Test");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

Error message:错误信息:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:830)
Caused by: java.lang.IllegalAccessError: superclass access check failed: class com.sun.javafx.scene.control.ControlHelper (in unnamed module @0x20a25f19) cannot access class com.sun.javafx.scene.layout.RegionHelper (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.scene.layout to unnamed module @0x20a25f19
    at java.base/java.lang.ClassLoader.defineClass1(Native Method)
    at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)
    at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:151)
    at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:821)
    at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:719)
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:642)
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:600)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    at javafx.scene.control.Control.<clinit>(Control.java:86)
    at assignment_2.exercise7.CompoundInterest.start(CompoundInterest.java:28)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    ... 1 more
Exception running application assignment_2.exercise7.CompoundInterest

Process finished with exit code 1

In Intellij Run -> Edit configurations Set module path to "--module-path "pathToJavaFx\javafx-sdk-13.0.2\lib" --add-modules javafx.controls,javafx.fxml"在 Intellij 运行 -> 编辑配置中将模块路径设置为"--module-path "pathToJavaFx\javafx-sdk-13.0.2\lib" --add-modules javafx.controls,javafx.fxml"

Is explained here https://openjfx.io/openjfx-docs/在这里解释https://openjfx.io/openjfx-docs/

Maven Maven

As the Getting Started page explains, you have a choice in how to build your JavaFX projects.入门页面所述,您可以选择如何构建 JavaFX 项目。

JavaFX 13 builds on top of JDK 13 and is a standalone component. JavaFX 13 建立在 JDK 13 之上,是一个独立的组件。 There are 2 different options for developing JavaFX applications:开发 JavaFX 应用程序有 2 个不同的选项:

  • Use the JavaFX SDK使用 JavaFX SDK

  • Use a build system (eg maven/gradle) to download the required modules from Maven Central.使用构建系统(例如 maven/gradle)从 Maven Central 下载所需的模块。

The other Answer seems to address the first.另一个答案似乎解决了第一个问题。 You may instead choose to make you project driven by a tool such as Maven .您可以改为选择让您的项目由Maven等工具驱动。

While learning a bit about Maven may seem like an annoying diversion, I have found it to be worth the time and effort, saving time with configuration and building of my projects.虽然学习一些关于 Maven 的知识似乎是一种烦人的消遣,但我发现它值得花时间和精力,节省了配置和构建我的项目的时间。 And Maven-driven projects are portable between the major IDE s.并且 Maven 驱动的项目在主要IDE之间是可移植的。

For instructions on using configuring your Maven POM file for OpenJFX work, see Run HelloWorld using Maven .有关为 OpenJFX 工作配置 Maven POM 文件的说明,请参阅使用 Maven 运行 HelloWorld

As shown on that documentation page, this XML element in your Maven POM will result in the necessary JavaFX classes, including Label , being automatically downloaded for use within your app.如该文档页面所示,您的 Maven POM 中的这个 XML 元素将导致必要的 JavaFX 类(包括Label )自动下载以在您的应用程序中使用。

<dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-controls</artifactId>
    <version>13</version>
</dependency>

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

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