不加载 fxml 文件

not loading fxml file

我用场景生成器为我的程序制作了简单的图形用户界面,它完美地加载了,但是当我通过场景生成器制作一个函数 handleButtonAction 并开始编译时,fxml 没有加载。我是场景生成器的新手,我不知道错误在哪里。

package sample;

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);
    }
}

这是我的控制器class

package sample;

import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;

public abstract class Controller implements Initializable {
    public Button btn_checkPSC;
    public Button btn_saveToCSV;
    public ScrollPane tbl_pane;
    public Button btn_insertRow;
    public Button btn_deleteAllSpace;
    public Button btn_1;
    public Button btn_2;
    public ProgressBar prgrs_bar;
    public TextField textarea;

    public Controller() {


    }


    public void handleButtonAction(javafx.event.ActionEvent actionEvent) {
        System.out.println("Working Handler");
    }
}

这是fxml.Probably这里有错误,但我使用的是 Intellij,他没有在 fxml 中显示错误

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

<?import javafx.geometry.*?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.ProgressBar?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.FlowPane?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>

<GridPane alignment="center" hgap="10" prefHeight="446.0" prefWidth="526.0" stylesheets="@sample.css" vgap="10" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <columnConstraints>
      <ColumnConstraints />
      <ColumnConstraints />
      <ColumnConstraints />
      <ColumnConstraints />
      <ColumnConstraints />
   </columnConstraints>
   <rowConstraints>
      <RowConstraints />
      <RowConstraints />
      <RowConstraints />
      <RowConstraints />
      <RowConstraints />
      <RowConstraints />
   </rowConstraints>
   <children>
      <FlowPane prefHeight="439.0" prefWidth="486.0" GridPane.columnIndex="1" GridPane.rowIndex="5">
         <children>
            <AnchorPane prefHeight="370.0" prefWidth="345.0">
               <children>
                  <ScrollPane fx:id="tbl_pane" layoutX="14.0" prefHeight="370.0" prefWidth="345.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="5.0" AnchorPane.topAnchor="0.0">
                     <content>
                        <TextField fx:id="textarea" prefHeight="352.0" prefWidth="325.0" text="jzozo" />
                     </content></ScrollPane>
               </children>
            </AnchorPane>
            <VBox alignment="TOP_CENTER" prefHeight="370.0" prefWidth="135.0">
               <children>
                  <Button fx:id="btn_checkPSC" mnemonicParsing="false" onAction="#handleButtonAction" text="Check PSC">
                     <VBox.margin>
                        <Insets top="5.0" />
                     </VBox.margin></Button>
                  <Button fx:id="btn_saveToCSV" mnemonicParsing="false" onAction="#handleButtonAction" text="Save To CSV">
                     <VBox.margin>
                        <Insets top="5.0" />
                     </VBox.margin></Button>
                  <Button fx:id="btn_insertRow" mnemonicParsing="false" onAction="#handleButtonAction" text="Insert Row">
                     <VBox.margin>
                        <Insets top="5.0" />
                     </VBox.margin></Button>
                  <Button fx:id="btn_deleteAllSpace" mnemonicParsing="false" onAction="#handleButtonAction" text="Delete All Spaces">
                     <VBox.margin>
                        <Insets top="5.0" />
                     </VBox.margin></Button>
                  <Button fx:id="btn_1" mnemonicParsing="false" onAction="#handleButtonAction" text="Button">
                     <VBox.margin>
                        <Insets top="5.0" />
                     </VBox.margin></Button>
                  <Button fx:id="btn_2" mnemonicParsing="false" onAction="#handleButtonAction" text="Button">
                     <VBox.margin>
                        <Insets top="5.0" />
                     </VBox.margin></Button>
                  <ProgressBar fx:id="prgrs_bar" prefWidth="200.0" progress="0.0" stylesheets="@sample.css">
                     <VBox.margin>
                        <Insets top="5.0" />
                     </VBox.margin></ProgressBar>
               </children>
            </VBox>
         </children></FlowPane>
      <MenuBar nodeOrientation="LEFT_TO_RIGHT" prefHeight="45.0" prefWidth="486.0" GridPane.columnIndex="1" GridPane.rowIndex="1">
        <menus>
          <Menu mnemonicParsing="false" text="File">
            <items>
              <MenuItem mnemonicParsing="false" text="Close" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Edit">
            <items>
              <MenuItem mnemonicParsing="false" text="Delete" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Help">
            <items>
              <MenuItem mnemonicParsing="false" text="About" />
            </items>
          </Menu>
        </menus>
      </MenuBar>
   </children>
</GridPane>

错误消息

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:473)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:372)
    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:945)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:973)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication(LauncherImpl.java:198)
    at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: javafx.fxml.LoadException: 
/C:/Users/MartinKranec/IdeaProjects/tmpBakalaris/out/production/tmpBakalaris/sample/sample.fxml:20

    at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
    at javafx.fxml/javafx.fxml.FXMLLoader.access0(FXMLLoader.java:105)
    at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:941)
    at javafx.fxml/javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:980)
    at javafx.fxml/javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:227)
    at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:752)
    at javafx.fxml/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2722)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3253)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3210)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3179)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3152)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3129)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3122)
    at sample.Main.start(Main.java:16)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1(LauncherImpl.java:919)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait(PlatformImpl.java:449)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:418)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:417)
    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(WinApplication.java:175)
    ... 1 more
Caused by: java.lang.InstantiationException
    at java.base/jdk.internal.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:488)
    at java.base/java.lang.Class.newInstance(Class.java:558)
    at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:936)
    ... 22 more
Exception running application sample.Main

FXMLLoader 通过实例化您在 FXML 根元素的 fx:controller 属性中指定的 class 创建控制器。如果这个class是抽象的,那当然不能实例化了。

您需要使控制器具体(而不是抽象)class。如果您想使用旧的 JavaFX2.1 之前的方法并实现 Initializable,那么您必须提供该接口中定义的方法:

package sample;

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

import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;

public class Controller implements Initializable {
    public Button btn_checkPSC;
    public Button btn_saveToCSV;
    public ScrollPane tbl_pane;
    public Button btn_insertRow;
    public Button btn_deleteAllSpace;
    public Button btn_1;
    public Button btn_2;
    public ProgressBar prgrs_bar;
    public TextField textarea;

    public Controller() {


    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }

    public void handleButtonAction(javafx.event.ActionEvent actionEvent) {
        System.out.println("Working Handler");
    }
}

在任何最新版本的 JavaFX 中,控制器根本不需要实现接口。因此,如果您不需要 initialize() 方法,请忽略它:

package sample;

import javafx.scene.control.Button;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;

public class Controller {
    public Button btn_checkPSC;
    public Button btn_saveToCSV;
    public ScrollPane tbl_pane;
    public Button btn_insertRow;
    public Button btn_deleteAllSpace;
    public Button btn_1;
    public Button btn_2;
    public ProgressBar prgrs_bar;
    public TextField textarea;

    public Controller() {


    }


    public void handleButtonAction(javafx.event.ActionEvent actionEvent) {
        System.out.println("Working Handler");
    }
}