Javafx:尽管有正确的名称和路径,但无法加载fxml

时间:2018-03-27 12:19:16

标签: java javafx

我是javafx的新手,我不明白为什么我的getResource无法打开fxml文件。我非常肯定我的名字和路径是正确的,但控制台显示

我已将错误消息更改为新消息

Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at application.Main.start(Main.java:31)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)

运行应用程序应用程序的异常。主要

这显然表明加载器的名称不正确或路径错误,我不相信我的情况。这是我的构建路径的图片

enter image description here

这是我的主要代码

package application;

import java.io.IOException;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.stage.Stage;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;

public class Main extends Application {

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

    @Override
    public void start(Stage primaryStage) throws Exception {
        // TODO Auto-generated method stub
        Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));

        Scene scene = new Scene(root);
        scene.getStylesheets().add(getClass().getResource("Main.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();

    }
}

这可能是我的fxml编码错误(用场景构建器制作)

很抱歉,如果这是一个非常简单的问题。

FXML代码(我删除了一些文本框以腾出30k字符的空间)

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

<?import javafx.geometry.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefWidth="1000.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controller.java">
   <top>
      <VBox prefHeight="200.0" prefWidth="100.0" BorderPane.alignment="CENTER">
         <children>
            <Label text="Attacker">
               <font>
                  <Font size="20.0" />
               </font>
            </Label>
            <GridPane prefHeight="60.0">
              <columnConstraints>
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="120.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
              </columnConstraints>
              <rowConstraints>
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
              </rowConstraints>
               <children>
                  <Label prefHeight="17.0" prefWidth="50.0" text="Units" />
                  <Label text="Amount" GridPane.rowIndex="1" />
                  <Label text="Losses" textFill="#fc0303" GridPane.rowIndex="2" />
                  <Label text="Spear" GridPane.columnIndex="1" />
                  <Label text="0" GridPane.columnIndex="1" GridPane.rowIndex="1" />
                  <Label text="0" textFill="RED" GridPane.columnIndex="1" GridPane.rowIndex="2" />
                  <Label text="Sword" GridPane.columnIndex="2" />
                  <Label text="0" GridPane.columnIndex="2" GridPane.rowIndex="1" />
                  <Label text="0" textFill="RED" GridPane.columnIndex="2" GridPane.rowIndex="2" />
                  <Label text="Axe" GridPane.columnIndex="3" />
                  <Label text="0" GridPane.columnIndex="3" GridPane.rowIndex="1" />
                  <Label text="0" textFill="RED" GridPane.columnIndex="3" GridPane.rowIndex="2" />
                  <Label text="Archer" GridPane.columnIndex="4" />
                  <Label text="0" GridPane.columnIndex="4" GridPane.rowIndex="1" />
                  <Label text="0" textFill="RED" GridPane.columnIndex="4" GridPane.rowIndex="2" />
                  <Label text="LC" GridPane.columnIndex="5" />
                  <Label text="0" GridPane.columnIndex="5" GridPane.rowIndex="1" />
                  <Label text="0" textFill="RED" GridPane.columnIndex="5" GridPane.rowIndex="2" />
                  <Label text="MA" GridPane.columnIndex="6" />
                  <Label text="0" GridPane.columnIndex="6" GridPane.rowIndex="1" />
                  <Label text="0" textFill="RED" GridPane.columnIndex="6" GridPane.rowIndex="2" />
                  <Label text="HC" GridPane.columnIndex="7" />
                  <Label text="0" GridPane.columnIndex="7" GridPane.rowIndex="1" />
                  <Label text="0" textFill="RED" GridPane.columnIndex="7" GridPane.rowIndex="2" />
                  <Label text="Ram" GridPane.columnIndex="8" />
                  <Label text="0" GridPane.columnIndex="8" GridPane.rowIndex="1" />
                  <Label text="0" textFill="RED" GridPane.columnIndex="8" GridPane.rowIndex="2" />
                  <Label text="Catapult" GridPane.columnIndex="9" />
                  <Label text="0" GridPane.columnIndex="9" GridPane.rowIndex="1" />
                  <Label text="0" textFill="RED" GridPane.columnIndex="9" GridPane.rowIndex="2" />
                  <Label text="Berserk" GridPane.columnIndex="10" />
                  <Label text="0" GridPane.columnIndex="10" GridPane.rowIndex="1" />
                  <Label text="0" textFill="RED" GridPane.columnIndex="10" GridPane.rowIndex="2" />
                  <Label text="Treb" GridPane.columnIndex="11" />
                  <Label text="0" GridPane.columnIndex="11" GridPane.rowIndex="1" />
                  <Label text="0" textFill="RED" GridPane.columnIndex="11" GridPane.rowIndex="2" />
                  <Label text="Noble" GridPane.columnIndex="12" />
                  <Label text="0" GridPane.columnIndex="12" GridPane.rowIndex="1" />
                  <Label text="0" textFill="RED" GridPane.columnIndex="12" GridPane.rowIndex="2" />
                  <Label text="Paladin" GridPane.columnIndex="13" />
                  <Label text="0" GridPane.columnIndex="13" GridPane.rowIndex="1" />
                  <Label text="0" textFill="RED" GridPane.columnIndex="13" GridPane.rowIndex="2" />
                  <Label text="Remaining" textFill="#02f858" GridPane.rowIndex="3" />
                  <Label text="0" textFill="LIME" GridPane.columnIndex="1" GridPane.rowIndex="3" />
                  <Label text="0" textFill="LIME" GridPane.columnIndex="2" GridPane.rowIndex="3" />
                  <Label text="0" textFill="LIME" GridPane.columnIndex="3" GridPane.rowIndex="3" />
                  <Label text="0" textFill="LIME" GridPane.columnIndex="4" GridPane.rowIndex="3" />
                  <Label text="0" textFill="LIME" GridPane.columnIndex="5" GridPane.rowIndex="3" />
                  <Label text="0" textFill="LIME" GridPane.columnIndex="6" GridPane.rowIndex="3" />
                  <Label text="0" textFill="LIME" GridPane.columnIndex="7" GridPane.rowIndex="3" />
                  <Label text="0" textFill="LIME" GridPane.columnIndex="8" GridPane.rowIndex="3" />
                  <Label text="0" textFill="LIME" GridPane.columnIndex="9" GridPane.rowIndex="3" />
                  <Label text="0" textFill="LIME" GridPane.columnIndex="10" GridPane.rowIndex="3" />
                  <Label text="0" textFill="LIME" GridPane.columnIndex="11" GridPane.rowIndex="3" />
                  <Label text="0" textFill="LIME" GridPane.columnIndex="12" GridPane.rowIndex="3" />
                  <Label text="0" textFill="LIME" GridPane.columnIndex="13" GridPane.rowIndex="3" />
               </children>
               <VBox.margin>
                  <Insets left="10.0" right="10.0" />
               </VBox.margin>
            </GridPane>
            <Label text="Defender">
               <font>
                  <Font size="20.0" />
               </font>
            </Label>
            <GridPane prefHeight="60.0">
               <children>
                  <Label prefHeight="17.0" prefWidth="50.0" text="Units" />
                  <Label text="Amount" GridPane.rowIndex="1" />
                  <Label text="Losses" textFill="#fc0303" GridPane.rowIndex="2" />
                  <Label text="Spear" GridPane.columnIndex="1" />
                  <Label text="0" GridPane.columnIndex="1" GridPane.rowIndex="1" />
                  <Label text="0" textFill="RED" GridPane.columnIndex="1" GridPane.rowIndex="2" />
                  <Label text="Sword" GridPane.columnIndex="2" />
                  <Label text="0" GridPane.columnIndex="2" GridPane.rowIndex="1" />
                  <Label text="0" textFill="RED" GridPane.columnIndex="2" GridPane.rowIndex="2" />
                  <Label text="Axe" GridPane.columnIndex="3" />
                  <Label text="0" GridPane.columnIndex="3" GridPane.rowIndex="1" />
                  <Label text="0" textFill="RED" GridPane.columnIndex="3" GridPane.rowIndex="2" />
                  <Label text="Archer" GridPane.columnIndex="4" />
                  <Label text="0" GridPane.columnIndex="4" GridPane.rowIndex="1" />
                  <Label text="0" textFill="RED" GridPane.columnIndex="4" GridPane.rowIndex="2" />
                  <Label text="LC" GridPane.columnIndex="5" />
                  <Label text="0" GridPane.columnIndex="5" GridPane.rowIndex="1" />
                  <Label text="0" textFill="RED" GridPane.columnIndex="5" GridPane.rowIndex="2" />
                  <Label text="MA" GridPane.columnIndex="6" />
                  <Label text="0" GridPane.columnIndex="6" GridPane.rowIndex="1" />
                  <Label text="0" textFill="RED" GridPane.columnIndex="6" GridPane.rowIndex="2" />
                  <Label text="HC" GridPane.columnIndex="7" />
                  <Label text="0" GridPane.columnIndex="7" GridPane.rowIndex="1" />
                  <Label text="0" textFill="RED" GridPane.columnIndex="7" GridPane.rowIndex="2" />
                  <Label text="Ram" GridPane.columnIndex="8" />
                  <Label text="0" GridPane.columnIndex="8" GridPane.rowIndex="1" />
                  <Label text="0" textFill="RED" GridPane.columnIndex="8" GridPane.rowIndex="2" />
                  <Label text="Catapult" GridPane.columnIndex="9" />
                  <Label text="0" GridPane.columnIndex="9" GridPane.rowIndex="1" />
                  <Label text="0" textFill="RED" GridPane.columnIndex="9" GridPane.rowIndex="2" />
                  <Label text="Berserk" GridPane.columnIndex="10" />
                  <Label text="0" GridPane.columnIndex="10" GridPane.rowIndex="1" />
                  <Label text="0" textFill="RED" GridPane.columnIndex="10" GridPane.rowIndex="2" />
                  <Label text="Treb" GridPane.columnIndex="11" />
                  <Label text="0" GridPane.columnIndex="11" GridPane.rowIndex="1" />
                  <Label text="0" textFill="RED" GridPane.columnIndex="11" GridPane.rowIndex="2" />
                  <Label text="Noble" GridPane.columnIndex="12" />
                  <Label text="0" GridPane.columnIndex="12" GridPane.rowIndex="1" />
                  <Label text="0" textFill="RED" GridPane.columnIndex="12" GridPane.rowIndex="2" />
                  <Label text="Paladin" GridPane.columnIndex="13" />
                  <Label text="0" GridPane.columnIndex="13" GridPane.rowIndex="1" />
                  <Label text="0" textFill="RED" GridPane.columnIndex="13" GridPane.rowIndex="2" />
                  <Label text="Remaining" textFill="#02f858" GridPane.rowIndex="3" />
                  <Label text="0" textFill="LIME" GridPane.columnIndex="1" GridPane.rowIndex="3" />
                  <Label text="0" textFill="LIME" GridPane.columnIndex="2" GridPane.rowIndex="3" />
                  <Label text="0" textFill="LIME" GridPane.columnIndex="3" GridPane.rowIndex="3" />
                  <Label text="0" textFill="LIME" GridPane.columnIndex="4" GridPane.rowIndex="3" />
                  <Label text="0" textFill="LIME" GridPane.columnIndex="5" GridPane.rowIndex="3" />
                  <Label text="0" textFill="LIME" GridPane.columnIndex="6" GridPane.rowIndex="3" />
                  <Label text="0" textFill="LIME" GridPane.columnIndex="7" GridPane.rowIndex="3" />
                  <Label text="0" textFill="LIME" GridPane.columnIndex="8" GridPane.rowIndex="3" />
                  <Label text="0" textFill="LIME" GridPane.columnIndex="9" GridPane.rowIndex="3" />
                  <Label text="0" textFill="LIME" GridPane.columnIndex="10" GridPane.rowIndex="3" />
                  <Label text="0" textFill="LIME" GridPane.columnIndex="11" GridPane.rowIndex="3" />
                  <Label text="0" textFill="LIME" GridPane.columnIndex="12" GridPane.rowIndex="3" />
                  <Label text="0" textFill="LIME" GridPane.columnIndex="13" GridPane.rowIndex="3" />
               </children>
               <columnConstraints>
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="120.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
               </columnConstraints>
               <rowConstraints>
                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
               </rowConstraints>
               <VBox.margin>
                  <Insets left="10.0" right="10.0" />
               </VBox.margin>
            </GridPane>
         </children>
      </VBox>
   </top>
   <bottom>
      <GridPane>
        <columnConstraints>
          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
        </columnConstraints>
        <rowConstraints>
          <RowConstraints maxHeight="24.0328369140625" minHeight="10.0" prefHeight="21.04925537109375" vgrow="SOMETIMES" />
          <RowConstraints maxHeight="38.95074462890625" minHeight="10.0" prefHeight="24.08203125" vgrow="SOMETIMES" />
          <RowConstraints maxHeight="38.95074462890625" minHeight="10.0" prefHeight="44.91796875" vgrow="SOMETIMES" />
        </rowConstraints>
         <children>
            <Label alignment="CENTER" contentDisplay="CENTER" text="Attacking losses" textAlignment="CENTER" GridPane.rowIndex="1" />
            <Label alignment="CENTER" contentDisplay="CENTER" text="Defending losses" GridPane.rowIndex="2" />
            <Label alignment="CENTER" contentDisplay="CENTER" text="Wood" GridPane.columnIndex="1" />
            <Label alignment="CENTER" contentDisplay="CENTER" text="Clay" GridPane.columnIndex="2" />
            <Label alignment="CENTER" contentDisplay="CENTER" text="Iron" GridPane.columnIndex="3" />
            <Label alignment="CENTER" contentDisplay="CENTER" text="Provision" GridPane.columnIndex="4" />
            <Label alignment="CENTER" contentDisplay="CENTER" text="Time spent" GridPane.columnIndex="5" />
            <Label alignment="CENTER" contentDisplay="CENTER" text="0" GridPane.columnIndex="1" GridPane.rowIndex="1" />
            <Label alignment="CENTER" contentDisplay="CENTER" text="0" GridPane.columnIndex="1" GridPane.rowIndex="2" />
            <Label alignment="CENTER" contentDisplay="CENTER" text="0" GridPane.columnIndex="2" GridPane.rowIndex="1" />
            <Label alignment="CENTER" contentDisplay="CENTER" text="0" GridPane.columnIndex="2" GridPane.rowIndex="2" />
            <Label alignment="CENTER" contentDisplay="CENTER" text="0" GridPane.columnIndex="3" GridPane.rowIndex="1" />
            <Label alignment="CENTER" contentDisplay="CENTER" text="0" GridPane.columnIndex="3" GridPane.rowIndex="2" />
            <Label alignment="CENTER" contentDisplay="CENTER" text="0" GridPane.columnIndex="4" GridPane.rowIndex="1" />
            <Label alignment="CENTER" contentDisplay="CENTER" text="0" GridPane.columnIndex="4" GridPane.rowIndex="2" />
            <Label alignment="CENTER" contentDisplay="CENTER" text="0" GridPane.columnIndex="5" GridPane.rowIndex="1" />
            <Label alignment="CENTER" contentDisplay="CENTER" text="0" GridPane.columnIndex="5" GridPane.rowIndex="2" />
            <Label text="Bashpoints gained" GridPane.columnIndex="6" />
            <Label text="0" GridPane.columnIndex="6" GridPane.rowIndex="1" />
            <Label text="0" GridPane.columnIndex="6" GridPane.rowIndex="2" />
         </children>
      </GridPane>
   </bottom>
   <center>
      <GridPane gridLinesVisible="true" BorderPane.alignment="CENTER">
        <columnConstraints>
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
        </columnConstraints>
        <rowConstraints>
            <RowConstraints maxHeight="42.0" minHeight="0.0" prefHeight="0.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        </rowConstraints>
         <children>
            <Label text="Attacker" GridPane.columnIndex="1" GridPane.rowIndex="1" />
            <Label text="Spearman" GridPane.rowIndex="2" />
            <Label text="Swordsman" GridPane.rowIndex="3" />
            <Label text="Axe Fighter" GridPane.rowIndex="4" />
            <Label text="Archer" GridPane.rowIndex="5" />
            <Label text="Light Cavalry" GridPane.rowIndex="6" />
            <Label text="Mounted Archer" GridPane.rowIndex="7" />
            <Label text="Heavy Cavalry" GridPane.rowIndex="8" />
            <Label text="Ram" GridPane.rowIndex="9" />
            <Label text="Catapult" GridPane.rowIndex="10" />
            <Label text="Berserker" GridPane.rowIndex="11" />
            <Label text="Nobleman" GridPane.rowIndex="13" />
            <Label text="Trebuchet" GridPane.rowIndex="12" />
            <Label text="Defender" GridPane.columnIndex="2" GridPane.rowIndex="1" />
            <Label text="Paladin" GridPane.rowIndex="14" />
            <TextField alignment="CENTER" text="0" GridPane.columnIndex="1" GridPane.rowIndex="2" />
            <TextField alignment="CENTER" text="0" GridPane.columnIndex="1" GridPane.rowIndex="3" />
            <TextField alignment="CENTER" text="0" GridPane.columnIndex="1" GridPane.rowIndex="7" />
            <TextField alignment="CENTER" text="0" GridPane.columnIndex="1" GridPane.rowIndex="6" />
            <TextField alignment="CENTER" text="0" GridPane.columnIndex="1" GridPane.rowIndex="5" />
            <TextField alignment="CENTER" text="0" GridPane.columnIndex="1" GridPane.rowIndex="4" />
            <TextField alignment="CENTER" text="0" GridPane.columnIndex="1" GridPane.rowIndex="11" />
            <TextField alignment="CENTER" text="0" GridPane.columnIndex="1" GridPane.rowIndex="10" />
            <TextField alignment="CENTER" text="0" GridPane.columnIndex="1" GridPane.rowIndex="9" />
            <TextField alignment="CENTER" text="0" GridPane.columnIndex="1" GridPane.rowIndex="8" />
            <TextField alignment="CENTER" text="0" GridPane.columnIndex="2" GridPane.rowIndex="2" />
            <TextField alignment="CENTER" text="0" GridPane.columnIndex="1" GridPane.rowIndex="14" />
            <TextField alignment="CENTER" text="0" GridPane.columnIndex="1" GridPane.rowIndex="13" />
            <TextField alignment="CENTER" text="0" GridPane.columnIndex="1" GridPane.rowIndex="12" />
            <TextField alignment="CENTER" text="0" GridPane.columnIndex="2" GridPane.rowIndex="9" />
            <TextField alignment="CENTER" text="0" GridPane.columnIndex="2" GridPane.rowIndex="8" />
            <TextField alignment="CENTER" text="0" GridPane.columnIndex="2" GridPane.rowIndex="7" />
            <TextField alignment="CENTER" text="0" GridPane.columnIndex="2" GridPane.rowIndex="6" />
            <TextField alignment="CENTER" text="0" GridPane.columnIndex="2" GridPane.rowIndex="5" />
            <TextField alignment="CENTER" text="0" GridPane.columnIndex="2" GridPane.rowIndex="4" />
            <TextField alignment="CENTER" text="0" GridPane.columnIndex="2" GridPane.rowIndex="3" />
            <TextField alignment="CENTER" text="0" GridPane.columnIndex="2" GridPane.rowIndex="14" />
            <TextField alignment="CENTER" text="0" GridPane.columnIndex="2" GridPane.rowIndex="13" />
            <TextField alignment="CENTER" text="0" GridPane.columnIndex="2" GridPane.rowIndex="12" />
            <TextField alignment="CENTER" text="0" GridPane.columnIndex="2" GridPane.rowIndex="10" />
            <TextField alignment="CENTER" text="0" GridPane.columnIndex="2" GridPane.rowIndex="11" />
            <ComboBox prefWidth="150.0" GridPane.columnIndex="4" GridPane.rowIndex="2" />
            <ComboBox prefWidth="150.0" GridPane.columnIndex="5" GridPane.rowIndex="2" />
            <Label text="Church Level" GridPane.columnIndex="3" GridPane.rowIndex="2" />
            <Label text="Morale (%)" GridPane.columnIndex="3" 
         </padding>
         <BorderPane.margin>
            <Insets />
         </BorderPane.margin>
      </GridPane>
   </center>
</BorderPane>

1 个答案:

答案 0 :(得分:0)

您的项目存在一些问题。

资源位置

您应该将FXML和CSS文件等资源放在@objc目录中。这就是它的用途。现在,您可以通过resources/访问您的资源了。请注意this.getClass().getClassLoader().getResource("resourcename.ext"):调用它会更舒服,因为您可以在每个资源的开头省略getClassLoader()

快速示例(Class vs. ClassLoader):

/

相当于

this.getClass().getClassLoader().getResource("someresource.txt");

您的项目结构现在应如下所示:

Project structure

注意:不要为this.getClass().getResource("/someresource.txt"); main/目录而烦恼,这只是我使用的maven标准。重要的是,您在java/ - 包中有Controller.javaMain.java,在application正下方有Main.cssMain.fxml。< / p>

注意:如果您拥有的文件多于主视图的FXML和CSS,则可能需要为每个视图添加目录。

<小时/> 您的resources/ - 方法现在应如下所示:

Application#start(Stage)

FXML中的第9行

@Override
public void start(Stage primaryStage) throws Exception
{
    Parent root = FXMLLoader.load(this.getClass().getClassLoader().getResource("Main.fxml"));

    Scene scene = new Scene(root);
    scene.getStylesheets().add(this.getClass().getClassLoader().getResource("Main.css").toExternalForm());
    primaryStage.setScene(scene);
    primaryStage.show();
}

应该是

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefWidth="1000.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controller.java">

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefWidth="1000.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controller"> - &gt; Controller.java 由于application.Controller属性采用&#34;完全限定的类名&#34; (packagename + classname)而不是文件路径作为参数。