添加控制器类时出现Javafx程序错误

添加控制器类时出现Javafx程序错误,java,javafx-8,Java,Javafx 8,我正在使用JavaFx和scenebuilder构建一个应用程序,但是除了添加Controller类之外,一切都很好 我得到以下错误: 应用程序启动方法中的异常线程“main”中的异常 java.lang.RuntimeException: com.sun.javafx.application.LaunchImpl.launchApplication1(LaunchImpl.java:875) 在 lambda$launchApplication$147(launchimpl.java:157)

我正在使用JavaFx和scenebuilder构建一个应用程序,但是除了添加Controller类之外,一切都很好

我得到以下错误:

应用程序启动方法中的异常线程“main”中的异常 java.lang.RuntimeException: com.sun.javafx.application.LaunchImpl.launchApplication1(LaunchImpl.java:875) 在 lambda$launchApplication$147(launchimpl.java:157) 在 com.sun.javafx.application.launchempl$$Lambda$1/868693306.run(未知 源代码)在java.lang.Thread.run(Thread.java:745)处,由以下原因引起: javafx.fxml.LoadException: /C:/Users/M%20ROSE/Documents/Info%20Trivia/out/production/Info%20Trivia/sample/gameScene1.fxml:15

然而,当我在fxml代码中从此行中删除fx:controller属性时,它就可以完美地工作

<BorderPane maxHeight="450.0" maxWidth="800.0" minHeight="450.0" minWidth="800.0" prefHeight="400.0" prefWidth="800.0" styleClass="questionInstance" stylesheets="@style.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
主类

package sample;

import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.shape.* ;
import javafx.geometry.* ;
import javafx.scene.text.* ;
import javafx.scene.image.* ;
import javafx.scene.control.* ;
import java.lang.* ;
import javafx.scene.layout.* ;
import javafx.geometry.Insets ;
import javafx.scene.layout.GridPane ;
import javafx.scene.control.Button ;
import javafx.scene.control.Label ;

public class Controller {

    //Initialize fxml controls
    public Button trueButton;
    public Button falseButton;
    public Label playerLabel;
    public Label questionLabel;
    public Label scoreValue;
    public ImageView questionImage;
    public Rectangle redBar;
    public Rectangle greenBar;


    //Create array for level 1 questions

    String[][] levelOneData = {
        {"This is a Sequence Diagram","f", null},
        {"This diagram is for a database","t", null},
        {"This is a rack diagram","t", null},
        {"This is a flow chart","f", null},
        {"This is a kind of UML diagram","t", null}
    };
    Image[] levelOneImages = new Image[]  {
            new Image("res/images/l1q1.png"),
            new Image("res/images/l1q2.png"),
            new Image("res/images/l1q3.png"),
            new Image("res/images/l1q4.png"),
            new Image("res/images/l1q5.png")
    };


    public void levelOneInitializer(){
        questionLabel.setText(levelOneData[0][0]);
        questionImage.setImage(levelOneImages[0]);
        System.out.println("done");

    }
}
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("gameScene1.fxml"));
        primaryStage.setTitle("Info Trivia");
        primaryStage.setScene(new Scene(root, 800, 450));
        primaryStage.setResizable(false);
        primaryStage.show();
    }


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

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

<?import javafx.scene.shape.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>

<BorderPane maxHeight="450.0" maxWidth="800.0" minHeight="450.0" minWidth="800.0" prefHeight="400.0" prefWidth="800.0" styleClass="questionInstance" stylesheets="@style.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <top>
      <HBox styleClass="questheader" BorderPane.alignment="CENTER">
         <children>
            <ImageView pickOnBounds="true" preserveRatio="true">
               <image>
                  <Image url="@res/images/userICon.png" />
               </image>
            </ImageView>
            <Label id="playerName" fx:id="playerLabel" text="Player 1" textFill="#a2c2b1">
               <font>
                  <Font name="Arial" size="24.0" />
               </font>
            </Label>
            <Region prefHeight="0.0" prefWidth="382.0" />
            <Label id="ScoreLabel" layoutX="40.0" layoutY="10.0" text="Score: " textFill="#a2c2b1">
               <font>
                  <Font name="Arial" size="24.0" />
               </font>
            </Label>
            <Label id="scoreValue" fx:id="scoreValue" layoutX="129.0" layoutY="10.0" text="0" textFill="#a2c2b1">
               <font>
                  <Font name="Arial" size="24.0" />
               </font>
            </Label>
         </children>
         <BorderPane.margin>
            <Insets left="50.0" right="50.0" />
         </BorderPane.margin>
         <padding>
            <Insets bottom="10.0" left="15.0" right="15.0" top="10.0" />
         </padding>
      </HBox>
   </top>
   <left>
      <StackPane prefHeight="150.0" prefWidth="200.0" BorderPane.alignment="CENTER">
         <BorderPane.margin>
            <Insets left="50.0" />
         </BorderPane.margin>
         <children>
            <ImageView fx:id="questionImage" pickOnBounds="true" preserveRatio="true">
               <image>
                  <Image url="@res/images/blankimage.png" />
               </image>
               <StackPane.margin>
                  <Insets left="35.0" />
               </StackPane.margin>
            </ImageView>
         </children>
      </StackPane>
   </left>
   <bottom>
      <HBox BorderPane.alignment="CENTER">
         <children>
            <Rectangle fx:id="greenBar" arcHeight="5.0" arcWidth="5.0" fill="#34b316" height="28.0" stroke="BLACK" strokeType="INSIDE" strokeWidth="0.0" width="10.0" />
            <Rectangle fx:id="redBar" arcHeight="5.0" arcWidth="5.0" fill="#a93535" height="28.0" stroke="BLACK" strokeType="INSIDE" strokeWidth="0.0" width="620.0" />

         </children>
         <padding>
            <Insets bottom="40.0" left="85.0" right="85.0" />
         </padding>
      </HBox>
   </bottom>
   <center>
      <BorderPane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
         <BorderPane.margin>
            <Insets right="85.0" />
         </BorderPane.margin>
         <top>
            <StackPane prefHeight="150.0" prefWidth="200.0" BorderPane.alignment="CENTER">
               <children>
                  <Label fx:id="questionLabel" text="This Is a Question" textFill="#191919">
                     <font>
                        <Font name="Arial" size="24.0" />
                     </font>
                  </Label>
               </children>
            </StackPane>
         </top>
         <center>
            <HBox prefHeight="100.0" prefWidth="200.0" BorderPane.alignment="CENTER">
               <children>
                  <Button fx:id="trueButton" mnemonicParsing="false" prefWidth="100.0" text="True">
                     <HBox.margin>
                        <Insets />
                     </HBox.margin>
                  </Button>
                  <Region prefWidth="200.0" />
                  <Button fx:id="falseButton" mnemonicParsing="false" prefWidth="100.0" text="False" />
               </children>
               <BorderPane.margin>
                  <Insets />
               </BorderPane.margin>
               <padding>
                  <Insets left="20.0" top="20.0" />
               </padding>
            </HBox>
         </center>
      </BorderPane>
   </center>
</BorderPane>

您必须实现可初始化控制器,请尝试以下操作:

public class Controller implement Initializable{

    //Initialize fxml controls
    @FXML
    public Button trueButton;
    @FXML
    public Button falseButton;
    @FXML
    public Label playerLabel;
    @FXML
    public Label questionLabel;
    @FXML
    public Label scoreValue;
    @FXML
    public ImageView questionImage;
    @FXML
    public Rectangle redBar;
    @FXML
    public Rectangle greenBar;


    //Create array for level 1 questions

    String[][] levelOneData = {
        {"This is a Sequence Diagram","f", null},
        {"This diagram is for a database","t", null},
        {"This is a rack diagram","t", null},
        {"This is a flow chart","f", null},
        {"This is a kind of UML diagram","t", null}
    };
    Image[] levelOneImages = new Image[]  {
            new Image("res/images/l1q1.png"),
            new Image("res/images/l1q2.png"),
            new Image("res/images/l1q3.png"),
            new Image("res/images/l1q4.png"),
            new Image("res/images/l1q5.png")
    };


    public void levelOneInitializer(){
        questionLabel.setText(levelOneData[0][0]);
        questionImage.setImage(levelOneImages[0]);
        System.out.println("done");

    }
}

我认为您需要仔细检查fxml中指定的控制器类的名称,并确保它与类的实际名称匹配。看起来您正在指定sample.Controller,并且您的类名是Controller。他们需要匹配。此外,名称中的“.”也不是一个好主意谢谢@RonSiven。我终于让它工作了,但这不是因为控制器的名称。似乎是我在控制器类中创建的数组导致了它。一旦我把它们注释掉,它就成功了。但是,我现在不知道如何才能创建这些阵列。如果您解决了问题,请添加答案并接受它(否则将被标记为未回答)@AdeoluFola Alade可能您的图像路径不正确?你没有跟踪吗?