当前位置: 首页 > 知识库问答 >
问题:

为什么我对启动简单代码(JavaFX)有问题?

堵德曜
2023-03-14

我从JavaFXml开始,但我在编译程序方面遇到了问题。编译器显示了我的问题,我没有任何想法来解决这个问题。

我的Mine类:

 package com.biku.readerFX;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }


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

我的控制器类:

package com.biku.readerFX;

public class Controller {
}

我的sample.fxml文件

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<VBox prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml"
      fx:controller="com.biku.readerFX.Controller">

    <Label text="I love bacon"/>
    <Button text="Submit"/>

</VBox>

当我编译这个时,compiller会显示一个错误:

  /usr/lib/jvm/java-11-openjdk-amd64/bin/java --module-path /home/biku/Pobrane/openjfx-13/javafx-sdk-13/lib --add-modules javafx.controls,javafx.fxml --add-modules javafx.base,javafx.graphics --add-reads javafx.base=ALL-UNNAMED --add-reads javafx.graphics=ALL-UNNAMED -javaagent:/snap/intellij-idea-community/177/lib/idea_rt.jar=41763:/snap/intellij-idea-community/177/bin -Dfile.encoding=UTF-8 -classpath /home/biku/IdeaProjects/WordReader/target/classes:/usr/lib/jvm/java-1.11.0-openjdk-amd64:/home/biku/.m2/repository/org/apache/maven/plugins/maven-compiler-plugin/3.8.1/maven-compiler-plugin-3.8.1.jar:/home/biku/.m2/repository/org/apache/maven/maven-plugin-api/3.0/maven-plugin-api-3.0.jar:/home/biku/.m2/repository/org/apache/maven/maven-model/3.0/maven-model-3.0.jar:/home/biku/.m2/repository/org/sonatype/sisu/sisu-inject-plexus/1.4.2/sisu-inject-plexus-1.4.2.jar:/home/biku/.m2/repository/org/sonatype/sisu/sisu-inject-bean/1.4.2/sisu-inject-bean-1.4.2.jar:/home/biku/.m2/repository/org/sonatype/sisu/sisu-guice/2.1.7/sisu-guice-2.1.7-noaop.jar:/home/biku/.m2/repository/org/apache/maven/maven-artifact/3.0/maven-artifact-3.0.jar:/home/biku/.m2/repository/org/codehaus/plexus/plexus-utils/2.0.4/plexus-utils-2.0.4.jar:/home/biku/.m2/repository/org/apache/maven/maven-core/3.0/maven-core-3.0.jar:/home/biku/.m2/repository/org/apache/maven/maven-settings/3.0/maven-settings-3.0.jar:/home/biku/.m2/repository/org/apache/maven/maven-settings-builder/3.0/maven-settings-builder-3.0.jar:/home/biku/.m2/repository/org/apache/maven/maven-repository-metadata/3.0/maven-repository-metadata-3.0.jar:/home/biku/.m2/repository/org/apache/maven/maven-model-builder/3.0/maven-model-builder-3.0.jar:/home/biku/.m2/repository/org/apache/maven/maven-aether-provider/3.0/maven-aether-provider-3.0.jar:/home/biku/.m2/repository/org/sonatype/aether/aether-impl/1.7/aether-impl-1.7.jar:/home/biku/.m2/repository/org/sonatype/aether/aether-spi/1.7/aether-spi-1.7.jar:/home/biku/.m2/repository/org/sonatype/aether/aether-api/1.7/aether-api-1.7.jar:/home/biku/.m2/repository/org/sonatype/aether/aether-util/1.7/aether-util-1.7.jar:/home/biku/.m2/repository/org/codehaus/plexus/plexus-interpolation/1.14/plexus-interpolation-1.14.jar:/home/biku/.m2/repository/org/codehaus/plexus/plexus-classworlds/2.2.3/plexus-classworlds-2.2.3.jar:/home/biku/.m2/repository/org/codehaus/plexus/plexus-component-annotations/1.5.5/plexus-component-annotations-1.5.5.jar:/home/biku/.m2/repository/org/sonatype/plexus/plexus-sec-dispatcher/1.3/plexus-sec-dispatcher-1.3.jar:/home/biku/.m2/repository/org/sonatype/plexus/plexus-cipher/1.4/plexus-cipher-1.4.jar:/home/biku/.m2/repository/org/apache/maven/shared/maven-shared-utils/3.2.1/maven-shared-utils-3.2.1.jar:/home/biku/.m2/repository/commons-io/commons-io/2.5/commons-io-2.5.jar:/home/biku/.m2/repository/org/apache/maven/shared/maven-shared-incremental/1.1/maven-shared-incremental-1.1.jar:/home/biku/.m2/repository/org/codehaus/plexus/plexus-java/0.9.10/plexus-java-0.9.10.jar:/home/biku/.m2/repository/org/ow2/asm/asm/6.2/asm-6.2.jar:/home/biku/.m2/repository/com/thoughtworks/qdox/qdox/2.0-M8/qdox-2.0-M8.jar:/home/biku/.m2/repository/org/codehaus/plexus/plexus-compiler-api/2.8.4/plexus-compiler-api-2.8.4.jar:/home/biku/.m2/repository/org/codehaus/plexus/plexus-compiler-manager/2.8.4/plexus-compiler-manager-2.8.4.jar:/home/biku/.m2/repository/org/codehaus/plexus/plexus-compiler-javac/2.8.4/plexus-compiler-javac-2.8.4.jar:/home/biku/.m2/repository/org/openjfx/javafx-controls/12.0.2/javafx-controls-12.0.2.jar:/home/biku/.m2/repository/org/openjfx/javafx-controls/12.0.2/javafx-controls-12.0.2-linux.jar:/home/biku/.m2/repository/org/openjfx/javafx-graphics/12.0.2/javafx-graphics-12.0.2.jar:/home/biku/.m2/repository/org/openjfx/javafx-graphics/12.0.2/javafx-graphics-12.0.2-linux.jar:/home/biku/.m2/repository/org/openjfx/javafx-base/12.0.2/javafx-base-12.0.2.jar:/home/biku/.m2/repository/org/openjfx/javafx-base/12.0.2/javafx-base-12.0.2-linux.jar:/home/biku/.m2/repository/org/openjfx/javafx-fxml/13/javafx-fxml-13.jar:/home/biku/.m2/repository/org/openjfx/javafx-fxml/13/javafx-fxml-13-linux.jar:/home/biku/.m2/repository/org/openjfx/javafx-maven-plugin/0.0.3/javafx-maven-plugin-0.0.3.jar:/home/biku/.m2/repository/org/codehaus/plexus/plexus-archiver/3.6.0/plexus-archiver-3.6.0.jar:/home/biku/.m2/repository/org/codehaus/plexus/plexus-io/3.0.1/plexus-io-3.0.1.jar:/home/biku/.m2/repository/org/apache/commons/commons-compress/1.16.1/commons-compress-1.16.1.jar:/home/biku/.m2/repository/org/objenesis/objenesis/2.6/objenesis-2.6.jar:/home/biku/.m2/repository/org/iq80/snappy/snappy/0.4/snappy-0.4.jar:/home/biku/.m2/repository/org/tukaani/xz/1.8/xz-1.8.jar:/home/biku/.m2/repository/org/apache/commons/commons-exec/1.3/commons-exec-1.3.jar:/home/biku/.m2/repository/org/twdata/maven/mojo-executor/2.3.0/mojo-executor-2.3.0.jar:/home/biku/.m2/repository/org/slf4j/slf4j-api/1.7.22/slf4j-api-1.7.22.jar:/home/biku/.m2/repository/org/slf4j/slf4j-simple/1.7.22/slf4j-simple-1.7.22.jar com.biku.readerFX.Main
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:566)
    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:566)
    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:834)
Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3230)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
    at com.biku.readerFX.Main.start(Main.java:13)
    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(Native Method)
    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.gtk.GtkApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)
    ... 1 more
Exception running application com.biku.readerFX.Main

Process finished with exit code 1

我试着解决它,就像在这一页上:

  • https://github.com/openjfx/javafx-maven-plugin
  • https://openjfx.io/openjfx-docs/#maven

并在VM选项路径上添加到FX:--module-path${PATH_TO_FX}--add-modules javafx.controls,javafx.fxml

向Project中添加类似lib的javafx。

你有什么办法帮我吗?THX

共有2个答案

公冶威
2023-03-14

好的,我有解决方案,路径是正确的,但我没有文件“project_name”.iml。该文件必须与pom.xml级别相同,直接位于project文件夹中。在这个文件里是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_11">
    <output url="file://$MODULE_DIR$/target/classes" />
    <output-test url="file://$MODULE_DIR$/target/test-classes" />
    <content url="file://$MODULE_DIR$">
      <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
      <sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
      <sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
      <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
      <excludeFolder url="file://$MODULE_DIR$/target" />
    </content>
金飞
2023-03-14

无论您的项目以何种方式设置,以下行:

Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));

无法提取“sample.fxml”。确保您的文件/路径规范的位置正确,否则您将始终遇到以下错误消息(位于堆栈跟踪的底部):

Caused by: java.lang.NullPointerException: Location is required.

TL;DR:“sample.fxml”的路径错误。您可能需要将它(sample.fxml文件)移动到与main.java文件相同的包中,或者将src/main/resources/目录移动到Maven安装中。我并不完全熟悉Gradle,但您也必须将fxml文件放在那里的resources根目录中。

 类似资料:
  • 这是:“Parent root=fxmlloader.load(getClass().getResource(”sample.fxml“));” 我不明白剩下的部分,所以我希望你能轻松地解决这个问题:)

  • 我试图自学JavaFX,并试图创建一个简单的笑脸图像。但出于某种原因,我所有的形状都以中心为中心,而不是以X&Y坐标为中心。我想不出为什么。有人能帮我弄清楚吗? 下面是我的代码: 这就是我最后得到的

  • 我拿不到输出。。有人能帮我得到输出吗 下面给出了程序运行的示例(注意:下面的粗体文本是用户输入的输入): 输入三角形的三条边

  • 问题内容: 我创建了一个简单的Angular JS $ routeProvider解决测试应用程序。它给出以下错误: 如果有人可以确定我哪里出了问题,我将不胜感激。 index.html ResolveTest.js rt.html 问题答案: 问题在于,当您在$ routeProvider中也为rt.html指定相同的控制器时,您就在index.html的body标签上了。从body标记中删除控

  • 我正在为我的discord机器人制作一个管理cog,我的代码无法识别“ctx”。PyCharm建议用“self”代替“ctx”,我不知道“self”是做什么的。从PyCharm所说的,还有数以百万计的其他东西,我必须写下它是什么。PyCharm无法识别帮会、发送、作者和频道,它还说是一个无法访问的代码。请注意,如果这似乎是一个非常愚蠢的问题,我是一个初学者,两周前就开始了。 至于代码: