来自 Main 的控制器 Class 中的 JavaFX 访问函数

JavaFX Access Function in Controller Class from Main

这让我用头撞墙!!!!希望有人能指出我正确的方向。我确定我正在做一些完全愚蠢和愚蠢的事情。我搜索了又搜索,但似乎无法找出为什么这不起作用!! (在 IntelliJ IDE 15.x 上使用 JDK8.x 和 Scenebuilder。我正在尝试在 GUI 上显示数据,但希望能够以编程方式从其他 classes/methods 将这些数据发送给它。这是一个简单的概念,我在处理我的大型项目之前试图完成工作:

Main Class:

package sample;

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

public class Main extends Application {

Controller myController = new Controller();

@Override
public void start(Stage primaryStage) throws Exception{
    Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
    primaryStage.setTitle("Hello World");
    primaryStage.setScene(new Scene(root, 345, 200));
    primaryStage.show();
    myController.pushBtn();
}

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

我的简单 GUI,使用 Scenebuilder Controller 在 FXML 中定义class:

package sample;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;

public class Controller {

@FXML // ResourceBundle that was given to the FXMLLoader
private ResourceBundle resources;

@FXML // URL location of the FXML file that was given to the FXMLLoader
private URL location;

@FXML // fx:id="txtBox"
private TextField txtBox; // Value injected by FXMLLoader

@FXML // fx:id="myButton"
private Button myButton; // Value injected by FXMLLoader

@FXML // This method is called by the FXMLLoader when initialization is   complete
void initialize() {
    assert txtBox != null : "fx:id=\"txtBox\" was not injected: check your   FXML file 'sample.fxml'.";
    assert myButton != null : "fx:id=\"myButton\" was not injected: check     our FXML file 'sample.fxml'.";
}
@FXML
private void pressBtn(ActionEvent event){
    txtBox.setText("Btn was pushed...");
}

@FXML
public void pushBtn(){
    txtBox.appendText("Sample from Main");
}

}

..最后是我非常简单的 FXML 文件,以及 ID 和事件处理程序标签:

<GridPane alignment="center" hgap="10" vgap="10"      xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.65"       fx:controller="sample.Controller">
  <columnConstraints>
  <ColumnConstraints />
  </columnConstraints>
  <rowConstraints>
    <RowConstraints />
 </rowConstraints>
 <children>
   <Pane prefHeight="200.0" prefWidth="345.0" style="-fx-background-color:  GRAY;">
       <children>
         <TextField fx:id="txtBox" layoutX="63.0" layoutY="28.0"     prefHeight="53.0" prefWidth="224.0" style="-fx-background-color: LIGHTGRAY;" />
          <Button fx:id="myButton" layoutX="140.0" layoutY="100.0"  mnemonicParsing="false" onAction="#pressBtn" text="Button" />
       </children>
      </Pane>
  </children>
 </GridPane>

我想做的就是调用已在控制器 class 上声明 public 的函数,并让该函数将文本放入我的 txtBox 中,该文本框已定义为如您所见,FXML。我在 main 中创建了该控制器 class 的一个实例,然后使用该实例访问该方法。我在这里阅读和搜索的所有内容都指出这是正确的方法。然而,这永远行不通,我得到的只是一个错误:

Exception in Application start method
Exception in thread "main" 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$launchApplication5(LauncherImp    l.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
    at sample.Controller.pushBtn(Controller.java:43)
at sample.Main.start(Main.java:20)
at    com.sun.javafx.application.LauncherImpl.lambda$launchApplication12(LauncherIm    pl.java:863)
at     com.sun.javafx.application.PlatformImpl.lambda$runAndWait5(PlatformImpl.java:326)
at    com.sun.javafx.application.PlatformImpl.lambda$null3(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at    com.sun.javafx.application.PlatformImpl.lambda$runLater4(PlatformImpl.java:294)
at     com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at     com.sun.glass.ui.win.WinApplication.lambda$null8(WinApplication.java:191)
... 1 more `

我到底错过了什么?有没有另一种方式来称呼它,我不是在初始化什么东西吗?非常感谢任何反馈!!!

调用 new Controller() 时,您正在创建第二个 Controller 实例。当然,这不是在您加载 FXML 文件时由 FXMLLoader 创建的 Controller 实例,因此它不会初始化任何 @FXML 注入的字段。

您需要从 FXMLLoader:

中获取 Controller 实例
package sample ;

import javafx.application.Application;
import javafx.application.Platform;
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{
        FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
        Parent root = loader.load();

        Controller myController = loader.getController();

        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 345, 200));
        primaryStage.show();
        myController.pushBtn();

   }

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