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

问题描述 投票:2回答:1

我正在使用JavaFx和场景生成器来构建应用程序,但是,除了添加Controller类之外,其他一切都正常。

我收到以下错误:

应用程序启动方法中的异常线程“主”中的异常java.lang.RuntimeException:应用程序启动方法中的异常,位于com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:875)在com.sun.javafx.application.LauncherImpl.lambda $ launchApplication $ 147(LauncherImpl.java:157)在com.sun.javafx.application.LauncherImpl $$ 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">

以下是相关代码

Controller Class

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

    }
}

Main Class

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>
java javafx-8
1个回答
0
投票

您的控制器必须实现Initializable,请尝试以下操作:

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

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.