【问题标题】:Javafx program error when controller class is added添加控制器类时Javafx程序错误
【发布时间】:2015-08-22 10:38:05
【问题描述】:

我正在使用 JavaFx 和场景构建器构建应用程序,但是一切正常,除非我添加了 Controller 类。

我收到以下错误:

应用程序启动方法中的异常线程“main”中的异常 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">

这里是相关代码

控制器类

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>

【问题讨论】:

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

标签: java javafx-8


【解决方案1】:

您的控制器必须实现 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");

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-20
    相关资源
    最近更新 更多