.getSource() returns javafx 上的空值

.getSource() returns null value on javafx

我正在做一个日历,如果我按一个 Label,第二个 window 就会显示出来以进行新的约会。

我一直都在使用一种接收事件的方法。当我尝试从此方法获取信息时(public String aux

public void labClick(MouseEvent event){

    try {

        Label aab=(Label) event.getSource();
        aux=aab.getText();
        Parent root;
        root = FXMLLoader.load(getClass().getResource("/application/cita.fxml"));
        Stage stage = new Stage();
        stage.setTitle("New Appoinment");
        stage.setScene(new Scene(root, 500, 500));
        stage.show();
    } catch (IOException e) {
        e.printStackTrace();
    }   
}

并尝试在这一个中使用:

public void insertCita(Event event){
    String dia;
    String inic;
    String fin;
    String stuf;
    Appoinment app;
    inic=startTime.getText();
    fin=finishTime.getText();
    stuf=stuff.getText();
    System.out.println(aux);
}

当我打印 aux 变量时,我得到一个 null 值。

有什么想法吗?

编辑:

这是 cita .fxml 文件

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

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

<AnchorPane prefHeight="185.0" prefWidth="341.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="application.Main.">
  <!-- TODO Add Nodes -->
  <children>
    <VBox layoutX="0.0" layoutY="0.0" prefHeight="185.0" prefWidth="341.0">
      <children>
        <TextField fx:id="startTime" prefHeight="35.0" prefWidth="341.0" text="Type starting time" />
        <TextField fx:id="finishTime" prefHeight="35.0" prefWidth="341.0" text="Type finishing time" />
        <TextField fx:id="stuff" prefHeight="35.0" prefWidth="341.0" text="Type stuff to do" />
        <Button fx:id="goBut" contentDisplay="LEFT" mnemonicParsing="false" onMouseClicked="#insertCita" prefHeight="52.0" prefWidth="90.0" text="Go" />
      </children>
    </VBox>
  </children>
</AnchorPane>

这是主要的 .fxml 文件:

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

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

<GridPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="application.Main">
  <children>
    <GridPane>
      <children>
        <GridPane>
          <children>
            <TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Monday" wrapText="true" GridPane.columnIndex="1" GridPane.rowIndex="0" />
            <TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Tuesday" wrapText="true" GridPane.columnIndex="2" GridPane.rowIndex="0" />
            <TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Wednesday" wrapText="true" GridPane.columnIndex="3" GridPane.rowIndex="0" />
            <TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Thursday" wrapText="true" GridPane.columnIndex="4" GridPane.rowIndex="0" />
            <TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Friday" wrapText="true" GridPane.columnIndex="5" GridPane.rowIndex="0" />
            <TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Saturday" wrapText="true" GridPane.columnIndex="6" GridPane.rowIndex="0" />
            <TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Sunday" wrapText="true" GridPane.columnIndex="7" GridPane.rowIndex="0" />
            <TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Monday" wrapText="true" GridPane.columnIndex="1" GridPane.rowIndex="9" />
            <TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Tuesday" wrapText="true" GridPane.columnIndex="2" GridPane.rowIndex="9" />
            <TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Wednesday" wrapText="true" GridPane.columnIndex="3" GridPane.rowIndex="9" />
            <TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Thursday" wrapText="true" GridPane.columnIndex="4" GridPane.rowIndex="9" />
            <TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Friday" wrapText="true" GridPane.columnIndex="5" GridPane.rowIndex="9" />
            <TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Saturday" wrapText="true" GridPane.columnIndex="6" GridPane.rowIndex="9" />
            <TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Sunday" wrapText="true" GridPane.columnIndex="7" GridPane.rowIndex="9" />
            <Button id="prev" fx:id="prev2" mnemonicParsing="false" onAction="#ClickMinus" prefHeight="30.0" prefWidth="70.0" text="prev" GridPane.columnIndex="8" GridPane.rowIndex="0" />
            <Button fx:id="next" mnemonicParsing="false" onAction="#ClickPlus" prefHeight="29.999900000002526" prefWidth="70.00009999999747" text="next" GridPane.columnIndex="0" GridPane.rowIndex="9" />
            <Button fx:id="next2" mnemonicParsing="false" onAction="#ClickPlus" prefHeight="30.0" prefWidth="70.0" text="next" GridPane.columnIndex="8" GridPane.rowIndex="9" />
            <Button fx:id="prev" mnemonicParsing="false" onAction="#ClickMinus" prefHeight="30.0" prefWidth="70.0" text="prev" GridPane.columnIndex="0" GridPane.rowIndex="0" />
            <TextArea fx:id="week1" prefWidth="200.0" text="Week x Year x" wrapText="true" GridPane.columnIndex="0" GridPane.rowIndex="2" />
            <TextArea fx:id="week2" prefWidth="200.0" text="Week x Year x" wrapText="true" GridPane.columnIndex="0" GridPane.rowIndex="4" />
            <TextArea fx:id="week4" prefWidth="200.0" text="Week x Year x" wrapText="true" GridPane.columnIndex="0" GridPane.rowIndex="8" />
            <Label fx:id="lab11" onMouseClicked="#labClick" prefHeight="40.0" prefWidth="150.0" text="" GridPane.columnIndex="1" GridPane.rowIndex="1" />
            <Label fx:id="lab12" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="2" GridPane.rowIndex="1" />
            <Label fx:id="lab13" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="3" GridPane.rowIndex="1" />
            <Label fx:id="lab14" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="4" GridPane.rowIndex="1" />
            <Label fx:id="lab15" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="5" GridPane.rowIndex="1" />
            <Label fx:id="lab21" minHeight="13.0" onMouseClicked="#labClick" prefHeight="40.0" prefWidth="149.9998779296875" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="3" />
            <Label fx:id="lab22" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="2" GridPane.rowIndex="3" />
            <Label fx:id="lab23" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="3" GridPane.rowIndex="3" />
            <Label fx:id="lab32" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="2" GridPane.rowIndex="5" />
            <Label fx:id="lab31" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="5" />
            <Label fx:id="lab33" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="3" GridPane.rowIndex="5" />
            <Label fx:id="lab34" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="4" GridPane.rowIndex="5" />
            <Label fx:id="lab24" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="4" GridPane.rowIndex="3" />
            <Label fx:id="lab25" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="5" GridPane.rowIndex="3" />
            <Label fx:id="lab35" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="5" GridPane.rowIndex="5" />
            <Label fx:id="lab41" onMouseClicked="#labClick" prefHeight="40.000099999997474" prefWidth="150.0" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="7" />
            <Label fx:id="lab42" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="2" GridPane.rowIndex="7" />
            <Label fx:id="lab16" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="6" GridPane.rowIndex="1" />
            <Label fx:id="lab17" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="7" GridPane.rowIndex="1" />
            <Label fx:id="lab26" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="6" GridPane.rowIndex="3" />
            <Label fx:id="lab43" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="3" GridPane.rowIndex="7" />
            <Label fx:id="lab44" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="4" GridPane.rowIndex="7" />
            <Label fx:id="lab45" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="5" GridPane.rowIndex="7" />
            <Label fx:id="lab36" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="6" GridPane.rowIndex="5" />
            <Label fx:id="lab46" onMouseClicked="#labClick" prefHeight="39.9998779296875" prefWidth="150.0" text="Label" GridPane.columnIndex="6" GridPane.rowIndex="7" />
            <Label fx:id="lab47" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="7" GridPane.rowIndex="7" />
            <Label fx:id="lab37" onMouseClicked="#labClick" prefHeight="40.000099999997474" prefWidth="139.0" text="Label" GridPane.columnIndex="7" GridPane.rowIndex="5" />
            <Label fx:id="lab27" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="7" GridPane.rowIndex="3" />
            <TextArea fx:id="week3" prefHeight="100.00009999999747" prefWidth="70.0" text="Week x Year x" wrapText="true" GridPane.columnIndex="0" GridPane.rowIndex="6" />
            <Button fx:id="start" mnemonicParsing="false" onAction="#ClickStart" prefHeight="30.0" prefWidth="70.0" text="Start" GridPane.columnIndex="0" GridPane.rowIndex="1" />
            <VBox fx:id="vb11" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="1" GridPane.rowIndex="2" />
            <VBox fx:id="vb12" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="2" GridPane.rowIndex="2" />
            <VBox id="vb12" fx:id="vb13" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="3" GridPane.rowIndex="2" />
            <VBox id="vb12" fx:id="vb21" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="1" GridPane.rowIndex="4" />
            <VBox id="vb12" fx:id="vb22" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="2" GridPane.rowIndex="4" />
            <VBox id="vb12" fx:id="vb23" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="3" GridPane.rowIndex="4" />
            <VBox id="vb12" fx:id="vb25" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="5" GridPane.rowIndex="4" />
            <VBox id="vb12" fx:id="vb31" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="1" GridPane.rowIndex="6" />
            <VBox id="vb12" fx:id="vb32" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="2" GridPane.rowIndex="6" />
            <VBox id="vb12" fx:id="vb14" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="4" GridPane.rowIndex="2" />
            <VBox id="vb12" fx:id="vb15" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="5" GridPane.rowIndex="2" />
            <VBox id="vb12" fx:id="vb35" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="5" GridPane.rowIndex="6" />
            <VBox id="vb12" fx:id="vb33" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="3" GridPane.rowIndex="6" />
            <VBox id="vb12" fx:id="vb41" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="1" GridPane.rowIndex="8" />
            <VBox id="vb12" fx:id="vb42" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="2" GridPane.rowIndex="8" />
            <VBox id="vb12" fx:id="vb43" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="3" GridPane.rowIndex="8" />
            <VBox id="vb12" fx:id="vb44" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="4" GridPane.rowIndex="8" />
            <VBox id="vb12" fx:id="vb45" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="5" GridPane.rowIndex="8" />
            <VBox id="vb12" fx:id="vb24" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="4" GridPane.rowIndex="4" />
            <VBox id="vb12" fx:id="vb26" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="6" GridPane.rowIndex="4" />
            <VBox id="vb12" fx:id="vb36" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="6" GridPane.rowIndex="6" />
            <VBox id="vb12" fx:id="vb16" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="6" GridPane.rowIndex="2" />
            <VBox id="vb12" fx:id="vb17" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="7" GridPane.rowIndex="2" />
            <VBox id="vb12" fx:id="vb27" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="7" GridPane.rowIndex="4" />
            <VBox id="vb12" fx:id="vb37" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="7" GridPane.rowIndex="6" />
            <VBox id="vb12" fx:id="vb34" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="4" GridPane.rowIndex="6" />
            <VBox id="vb12" fx:id="vb46" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="6" GridPane.rowIndex="8" />
            <VBox id="vb12" fx:id="vb47" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="7" GridPane.rowIndex="8" />
          </children>
          <columnConstraints>
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="70.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="150.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="150.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="150.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="150.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="150.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="150.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="150.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="70.0" />
          </columnConstraints>
          <rowConstraints>
            <RowConstraints maxHeight="40.0" minHeight="10.0" prefHeight="40.0" vgrow="SOMETIMES" />
            <RowConstraints maxHeight="40.0" minHeight="10.0" prefHeight="40.0" vgrow="SOMETIMES" />
            <RowConstraints maxHeight="100.0" minHeight="10.0" prefHeight="100.0" vgrow="SOMETIMES" />
            <RowConstraints maxHeight="40.0" minHeight="10.0" prefHeight="40.0" vgrow="SOMETIMES" />
            <RowConstraints maxHeight="100.0" minHeight="10.0" prefHeight="100.0" vgrow="SOMETIMES" />
            <RowConstraints maxHeight="40.0" minHeight="10.0" prefHeight="40.0" vgrow="SOMETIMES" />
            <RowConstraints maxHeight="100.0" minHeight="10.0" prefHeight="100.0" vgrow="SOMETIMES" />
            <RowConstraints maxHeight="40.0" minHeight="10.0" prefHeight="40.0" vgrow="SOMETIMES" />
            <RowConstraints maxHeight="100.0" minHeight="10.0" prefHeight="100.0" vgrow="SOMETIMES" />
            <RowConstraints maxHeight="40.0" minHeight="10.0" prefHeight="40.0" vgrow="SOMETIMES" />
          </rowConstraints>
        </GridPane>
      </children>
      <columnConstraints>
        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
      </columnConstraints>
      <rowConstraints>
        <RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
      </rowConstraints>
    </GridPane>
  </children>
  <columnConstraints>
    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
  </columnConstraints>
  <rowConstraints>
    <RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
  </rowConstraints>
</GridPane>

labClick方法中设置aux值,在主FXML文件的控制器中调用该方法;然而,您随后试图在 cita.fxml 的控制器中访问它,它从未被初始化。

您对两个 FXML 文件中的控制器使用了相同的 class,这总是会导致这种混淆,因为很难跟踪哪些变量在哪些控制器中被初始化。我强烈建议您为每个 FXML 文件的控制器使用不同的 class。

无论哪种方式,您都需要在加载时将值从一个控制器传递到另一个控制器 cita.fxmlPassing Parameters JavaFX FXML 中概述了执行此操作的方法,但您可以简单地定义一个 CitaController class 以与 cita.fxml 一起使用,给它一个 setAux() 方法:

public class CitaController {

    private String aux ;

    public void setAux(String aux) {
        this.aux = aux ;
    }

    @FXML
    private void insertCita(Event event){ 
        // existing code...
    }

    // ...
}

然后用

加载FXML
Label aab=(Label) event.getSource();
aux=aab.getText();
FXMLLoader loader = new FXMLLoader(getClass().getResource("/application/cita.fxml"));
Parent root = loader.load();

CitaController citaController = loader.getController();
citaController.setAux(aux);

// ...