Javafx java.lang.reflect.InvocationTargetException当我尝试更改标签时';s文本值

Javafx java.lang.reflect.InvocationTargetException当我尝试更改标签时';s文本值,javafx,fxml,Javafx,Fxml,下面是一个使用fxml和netbeans的javafx的简单标签项目。 问题是,当我使用 StartFomulars.setText("abc"); 它只是得到下面给出的一个异常 这是main的代码 package main; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import ja

下面是一个使用fxml和netbeans的javafx的简单标签项目。 问题是,当我使用

StartFomulars.setText("abc");
它只是得到下面给出的一个异常

这是main的代码

package main;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;


public class Start extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Prove Controller = new Prove();
        Controller.launchController(stage);
    }

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

}
fxml

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

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="720.0" prefWidth="1080.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">
  <children>
    <GridPane layoutX="0.0" layoutY="0.0" prefHeight="720.0" prefWidth="1080.0">
      <children>
        <GridPane GridPane.columnIndex="0" GridPane.rowIndex="0">
          <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 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>
        </GridPane>
        <GridPane GridPane.columnIndex="1" GridPane.rowIndex="1">
          <children>
            <GridPane GridPane.columnIndex="0" GridPane.rowIndex="0">
              <columnConstraints>
                <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 minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
              </rowConstraints>
            </GridPane>
          </children>
          <columnConstraints>
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
          </columnConstraints>
          <rowConstraints>
            <RowConstraints maxHeight="282.0" minHeight="10.0" prefHeight="282.0" vgrow="SOMETIMES" />
            <RowConstraints maxHeight="281.0" minHeight="10.0" prefHeight="281.0" vgrow="SOMETIMES" />
          </rowConstraints>
        </GridPane>
        <GridPane GridPane.columnIndex="0" GridPane.rowIndex="1">
          <children>
            <Label text="Given: " GridPane.columnIndex="0" GridPane.rowIndex="0" />
            <Label text="Prove: " GridPane.columnIndex="0" GridPane.rowIndex="1" />
            <Label text="Goal: " GridPane.columnIndex="0" GridPane.rowIndex="2" />
            <Label fx:id="StartFomulars" text="StartFomulars" GridPane.columnIndex="1" GridPane.rowIndex="0" />
            <Label fx:id="GoalFomular" text="GoalFomular" GridPane.columnIndex="1" GridPane.rowIndex="2" />
          </children>
          <columnConstraints>
            <ColumnConstraints hgrow="SOMETIMES" maxWidth="457.0" minWidth="10.0" prefWidth="125.0" />
            <ColumnConstraints hgrow="SOMETIMES" maxWidth="802.0" minWidth="10.0" prefWidth="790.0" />
          </columnConstraints>
          <rowConstraints>
            <RowConstraints maxHeight="188.0" minHeight="10.0" prefHeight="156.0" vgrow="SOMETIMES" />
            <RowConstraints maxHeight="364.0" minHeight="10.0" prefHeight="364.0" vgrow="SOMETIMES" />
            <RowConstraints maxHeight="186.0" minHeight="10.0" prefHeight="43.0" vgrow="SOMETIMES" />
          </rowConstraints>
        </GridPane>
      </children>
      <columnConstraints>
        <ColumnConstraints hgrow="SOMETIMES" maxWidth="915.0" minWidth="10.0" prefWidth="915.0" />
        <ColumnConstraints hgrow="SOMETIMES" maxWidth="539.0" minWidth="10.0" prefWidth="165.0" />
      </columnConstraints>
      <rowConstraints>
        <RowConstraints maxHeight="359.0" minHeight="10.0" prefHeight="157.0" vgrow="SOMETIMES" />
        <RowConstraints maxHeight="582.0" minHeight="10.0" prefHeight="563.0" vgrow="SOMETIMES" />
      </rowConstraints>
    </GridPane>
  </children>
</AnchorPane>

你的结构很不寻常。如图所示,构造应用程序类、FXML文件和控制器更为正常

设置好之后,控制器类和FXML之间没有连接。因此,注释为
@FXML
的字段永远不会初始化

其工作方式是,
fxmloader
load(…)
方法将解析FXML文件。如果加载器上设置了控制器,则定义了
fx:id
属性的任何元素都将被注入控制器中匹配的
@FXML
注释字段中

FXMLLoader
上设置控制器基本上有两种方法。第一个是在FXML文件的根元素上定义一个
fx:controller
属性。这将导致FXML加载程序创建指定类的新实例,并将其用作其控制器。注意,这在您的情况下不起作用,因为您希望控制器是您已经创建的
Prove
实例,而不是
Prove
类的新实例

第二种方法是创建
fxmloader
实例(而不是依赖当前使用的
static
fxmloader.load(URL)
方法),并在该实例上调用
setController(…)
。所以(我认为)取消注释

//fxmlLoader.setController(this);
这条线会让它工作的

您可能还需要分隔字段的声明,即:

@FXML
private Label StartFomulars ;
@FXML
private Label GoalFomular; 
(如果没有这种改变,它可能会起作用,我从来没有像你那样尝试过。)


您可能还应该在
Prove
的构造函数中取出
try{…}catch(…){…}
结构(因为您声明它
抛出IOException
),或者至少在
catch
块中执行一些操作。现在,如果加载FXML时发生异常,您将不知道它,您只需继续尝试并设置标签的文本。

您的结构非常不寻常。如图所示,构造应用程序类、FXML文件和控制器更为正常

设置好之后,控制器类和FXML之间没有连接。因此,注释为
@FXML
的字段永远不会初始化

其工作方式是,
fxmloader
load(…)
方法将解析FXML文件。如果加载器上设置了控制器,则定义了
fx:id
属性的任何元素都将被注入控制器中匹配的
@FXML
注释字段中

FXMLLoader
上设置控制器基本上有两种方法。第一个是在FXML文件的根元素上定义一个
fx:controller
属性。这将导致FXML加载程序创建指定类的新实例,并将其用作其控制器。注意,这在您的情况下不起作用,因为您希望控制器是您已经创建的
Prove
实例,而不是
Prove
类的新实例

第二种方法是创建
fxmloader
实例(而不是依赖当前使用的
static
fxmloader.load(URL)
方法),并在该实例上调用
setController(…)
。所以(我认为)取消注释

//fxmlLoader.setController(this);
这条线会让它工作的

您可能还需要分隔字段的声明,即:

@FXML
private Label StartFomulars ;
@FXML
private Label GoalFomular; 
(如果没有这种改变,它可能会起作用,我从来没有像你那样尝试过。)

您可能还应该在
Prove
的构造函数中取出
try{…}catch(…){…}
结构(因为您声明它
抛出IOException
),或者至少在
catch
块中执行一些操作。现在,如果加载FXML时发生异常,您将不知道它,您只需继续尝试并设置标签的文本