为什么当我使用 FXML 以编程方式在 JavaFX 中触发事件时控制对象为空

Why control object is null when I programmatically fire event in JavaFX with FXML

我正在尝试通过 KeyPress 事件触发现有的按钮点击事件。 它仅在 FXML 进行控制时失败。 例如,this sample 确实可以正常工作。但是当我使用 FXMLKeyPressed 事件触发按钮单击事件制作几乎相同的视图和控件时,按钮变为空。 为什么会这样?我忘记了什么,一些基本的东西?

一个 KeyPress 事件有效,但是当触发按钮事件时它抛出 NullpointerException。

这是简单的示例 java 代码。

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.stage.Stage;

import java.io.IOException;

public class RapidFireFXMLEdition extends Application {

    @FXML
    private Button button;
    @FXML
    private Label label;

    private static int nClicks = 0;

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

    @Override
    public void start(Stage primaryStage) throws IOException{
        Parent root = FXMLLoader.load(getClass().getResource("rapidfire.fxml"));
        Scene scene = new Scene(root);

        scene.setOnKeyPressed(event -> handleKeyPressed(event));
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void handleKeyPressed(KeyEvent event) {
        KeyCode keyCode = event.getCode();
        System.out.println("handleKeyPressed, keyCode: " + keyCode);
        if (keyCode == KeyCode.F1) {
            button.fire();  // button turns null 
        }

    }

    @FXML
    private void handleClick() {
        nClicks++;
        System.out.println("Clicked " + nClicks + " times.");
        label.setText("Clicked " + nClicks + " times.");
    }   
}

和 fxml 文件。

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>

<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="90.0" prefWidth="100.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="rapid_sample.RapidFireFXMLEdition">
   <children>
      <Button fx:id="button" mnemonicParsing="false" onAction="#handleClick" text="Click Me!" />
      <Label fx:id="label" text="display count here" />
      <BorderPane prefHeight="200.0" prefWidth="400.0" />
   </children>
</VBox>

更新:

这里只是备忘回答。我使用 FXML 使 KeyPressd 事件触发点击事件成为可能,并且我从应用程序 class.

中分离出一个控制器

RapidCleanApp.java:

package rapid_sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

import java.io.IOException;

public class RapidCleanApp extends Application {

    public static Stage primaryStage;
    private BorderPane rootLayout;

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

    @Override
    public void start(Stage primaryStage) throws IOException {

        this.primaryStage = primaryStage;
        this.primaryStage.setTitle("Rapid but Clean App");
        initRootLayout();
        this.primaryStage.show();
    }

    private void initRootLayout() throws IOException {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(RapidCleanApp.class.getResource("rapidclean.fxml"));
        rootLayout = loader.load();
        Scene scene = new Scene(rootLayout);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

}

RapidCleanAppController.java:

package rapid_sample;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;

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

public class RapidCleanAppController implements Initializable{

    @FXML
    private BorderPane pane;
    @FXML
    private Button button;
    @FXML
    private Label label;

    private static int nClicks = 0;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        System.out.println("Controller is initializing");
    }

    @FXML
    private void handleKeyPressed(KeyEvent event) {
        KeyCode keyCode = event.getCode();
        System.out.println("handleKeyPressed, keyCode: " + keyCode);
        if (keyCode == KeyCode.F1) {
            button.fire();
        }
    }

    @FXML
    private void handleClick() {
        nClicks++;
        System.out.println("Clicked " + nClicks + " times.");
        label.setText("Clicked " + nClicks + " times.");
    }

}

rapidclean.fxml:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>

<BorderPane fx:id="pane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" onKeyPressed="#handleKeyPressed" prefHeight="100.0" prefWidth="200.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1"
            fx:controller="rapid_sample.RapidCleanAppController">
   <center>
      <VBox prefHeight="200.0" prefWidth="100.0" BorderPane.alignment="CENTER">
         <children>
            <Button fx:id="button" mnemonicParsing="false" onAction="#handleClick" prefHeight="23.0" prefWidth="96.0" text="Click Me!" />
            <Label fx:id="label" text="display count here" />
         </children>
      </VBox>
   </center>
</BorderPane>

我不确定应用程序 class 是否也可以作为控制器。最好将 Controller 移动到 separetad class。 或者其他丑陋的选择是: 添加:

public Button getButton() {
    return this.button;
}

然后在 scene.setOnKeyPressed 之前:

RapidFireFXMLEdition controller = loader.getController();
this.button = controller.getButton();

我不建议使用 Application class 作为控制器;恕我直言 Application class 应该只 "assemble" 不同的部分,不包含太多逻辑,因为在 Application class 中重用代码在某种程度上受到应用程序的限制生命周期。仍然有很多方法可以与控制器 class 通信,如下所述:Passing Parameters JavaFX FXML

但是,如果您使用 Application class 作为控制器,您应该知道使用 fx:controller 将创建一个 新实例,不使用现有的。要使用现有的,需要在加载场景时指定控制器实例,并去掉fxml中的fx:controller属性:

FXMLLoader loader = new FXMLLoader(getClass().getResource("rapidfire.fxml"));
loader.setController(this);
Parent root = loader.load();
...
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="90.0" prefWidth="100.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1">
...