当前位置: 首页 > 知识库问答 >
问题:

.getSource()在javafx上返回空值

林鹏鹍
2023-03-14
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);
}

这是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>

共有1个答案

滕成双
2023-03-14

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

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

无论哪种方式,在加载cita.fxml时,都需要将值从一个控制器传递到另一个控制器。在传递参数JavaFX FXML中概述了实现这一目的的方法,但是简单地说,您应该定义一个Citacontroller类,用于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...
    }

    // ...
}
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);

// ...
 类似资料:
  • 我正在使用AWS Amplify在iOS和Android上构建一个应用程序。我有一个lambda表达式,它接受经度、纬度、用户名和对象id,并返回一个布尔值,告诉我用户是否在物理上接近该对象。 它完美地工作在我的iOS应用程序,我得到或取决于用户位置,但在Android上,我总是得到作为响应。 这是触发lambda的函数: 当我打印日志的参数发送到lambda一切正常,正确的值被发送。 这是我的回

  • 我在过去的两个小时里一直在试图解决这个问题。试过很多东西。 该片段似乎正在向我的适配器传递空上下文。我尝试在onCreate和onCreateView和onActivityCreated中初始化上下文变量。同样的结果。

  • 通过下面的代码片段,我试图运行一个查询,该查询更新数据或将新数据插入名为的表中。该表包含一个名为和的列。如果中已经有一个节点,则更新中以毫秒为单位的时间。否则,将插入新的信息。 问题是,下面的代码片段无法将数据插入到数据库的表中。原因是声明: 那么应该如何编辑代码,以便更新重复值并插入新值呢?

  • 问题内容: 我创建了一个简单的JavaFX应用程序,该应用程序在TextField中接收来自用户的输入。我将来自SceneBuilder的KeyTyped事件附加到控制器。我的函数如下所示: 当我键入回车键时,此功能始终打印出UNDEFINED。有想法该怎么解决这个吗?我键入的其他字母似乎也有相同的问题。 问题答案: 是一个特殊事件。它没有但已设置。 请参见字母“ a”的示例: 和javadoc:

  • 问题内容: 我刚刚开始使用JavaFX Scene Builder来构建一个小型应用程序。 它由属于“ login.fxml”的控制器类“ Login.java”组成,其中通过称为“ registrationClicked(ActionEvent event)”的方法加载了FXML文件“ registrierung.fxml”: 现在,我想通过根节点vboxRoot引用控制器类“ Registri

  • 我刚刚开始使用JavaFX Scene Builder来构建一个小型应用程序。 它由属于login.fxml的控制器类Login.java组成,其中FXML文件registrierung.fxml通过名为注册点击(动作事件)的方法加载: 现在我想参考一下“registrierung”的阶段。fxml“在控制器类”注册表中。java“通过根节点vboxRoot: 但是,“getScene()”总是导