使用JavaFX和SceneBuilder构建的小程序无法运行

StackOverFlow经常为我提供帮助,但是我从来没有想过,我会自己写在这里,尤其是因为我在乱扔英语方面挣扎。但是我会尝试,因为我正在学习JavaFX上的一些视频课程,但是至少有两天没能继续学习,因为我无法自行解决此问题。这是关于在Java(FX)主类,控制器类和中间fxml文件之间建立连接。

所以,我试图建立一个这样的舞台和场景:

package abcxyz;

import java.net.URL;

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

public class TestSBMain extends Application {
    
    @Override
    public void start(Stage primaryStage) throws Exception {
        URL url = getclass().getResource("hallo.fxml");
        Parent root = FXMLLoader.load(url);
        Scene scene = new Scene (root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    
    public static void main(String[] args) {
        launch(args);}
}

如您所见,我将start方法与一个由场景构建器构建的fxml文件相连接。请看一下:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>


<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" 
xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="abcxyz.TestController">
   <children>
      <Button fx:id="halloButton" layoutX="258.0" layoutY="187.0" mnemonicParsing="false" text="Hallo" />
   </children>
</AnchorPane>

如您所见,该文件链接到控制器类,该类应该控制已建立的JavaFX-Button。当我也显示此内容时,请耐心等待我:

package abcxyz;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;

public class TestController implements Initializable {
    @FXML
    private Button halloButton;

    @Override
    public void initialize(URL arg0,ResourceBundle arg1) {
        halloButton.setDisable(true);
    }

}

但是我收到此错误消息,我已经两天没来了:

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:564)
    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:564)
    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:832)
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 abcxyz.TestSBMain.start(TestSBMain.java:16)
    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)
Exception running application abcxyz.TestSBMain

我的程序是这样分层的:

enter image description here

您能看到什么是探棒吗?我将非常感谢您提供解决方案或一些小建议!提前非常感谢您!很抱歉我的第一篇文章太长了! :-)

祝一切顺利, 基督徒

PeterChia 回答:使用JavaFX和SceneBuilder构建的小程序无法运行

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/1368040.html

大家都在问