JavaFX 更改标签文本

JavaFX change label text

我正在尝试为用 Java 编写的应用程序制作 GUI。

我使用 Scene Builder 制作了 fxml 文档,正确设置了 fx:id,现在我正在尝试对表单进行简单的更改。

我的文档控制器:

public class FXMLDocumentController implements Initializable {
@FXML    
Label LabelDatum;



@Override
public void initialize(URL url, ResourceBundle rb) {
     LabelDatum.setText((new Date()).toString());
}    
}

我的 FX 主文件:

public class FXMain extends Application {

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

   primaryStage.setScene(scene);
   primaryStage.show();
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}    
}

我现在想要的一切,就是将 LabelDatum 设置为实际时间戳,但是当我 运行 FX 主文件时,没有任何反应。 有没有人可以帮助我?

谢谢。 保罗

更新:

我的整个 FXMLDocument:

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

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

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="478.0" prefWidth="682.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <top>
      <AnchorPane prefHeight="116.0" prefWidth="600.0" BorderPane.alignment="CENTER">
         <children>
            <Label fx:id="LabelNadpis" layoutX="14.0" layoutY="16.0" text="Jídelní lístek Dne">
               <font>
                  <Font name="System Bold" size="24.0" />
               </font>
            </Label>
            <Label fx:id="LabelDatum" layoutX="237.0" layoutY="18.0" prefHeight="32.0" prefWidth="127.0" text="datum">
               <font>
                  <Font size="22.0" />
               </font>
            </Label>
            <Label fx:id="LabelNazevRestauraceNadpis" layoutX="14.0" layoutY="55.0" prefHeight="17.0" prefWidth="99.0" text="Název restaurace" />
            <Label fx:id="LabelSestavilNadpis" layoutX="14.0" layoutY="72.0" text="Sestavil" />
            <Label fx:id="LabelNazevRestauraceHodnota" layoutX="237.0" layoutY="55.0" text="nazev_restaurace" />
            <Label fx:id="LabelSestavilHodnota" layoutX="237.0" layoutY="72.0" text="sestavil" />
         </children>
      </AnchorPane>
   </top>
   <center>
      <ListView fx:id="ListViewPokrmy" prefHeight="231.0" prefWidth="600.0" BorderPane.alignment="CENTER" />
   </center>
   <bottom>
      <AnchorPane prefHeight="46.0" prefWidth="696.0" BorderPane.alignment="CENTER">
         <children>
            <Button fx:id="ButtonNactiTxt" layoutX="26.0" layoutY="12.0" mnemonicParsing="false" text="Načti txt" />
            <Button fx:id="ButtonVlozNovy" layoutX="97.0" layoutY="12.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="83.0" text="Vlož nový" />
            <Button fx:id="ButtonOdeber" layoutX="188.0" layoutY="12.0" mnemonicParsing="false" text="Odeber" />
            <Button fx:id="ButtonEditace" layoutX="250.0" layoutY="12.0" mnemonicParsing="false" text="Editace" />
            <ChoiceBox layoutX="316.0" layoutY="12.0" prefWidth="150.0" />
            <Button fx:id="ButtonZrusListek" layoutX="472.0" layoutY="11.0" mnemonicParsing="false" text="Zruš lístek" />
            <Button fx:id="ButtonUloz" layoutX="550.0" layoutY="11.0" mnemonicParsing="false" text="Ulož" />
            <Button fx:id="ButtonObnov" layoutX="597.0" layoutY="11.0" mnemonicParsing="false" text="Obnov" />
         </children>
      </AnchorPane>
   </bottom>
</BorderPane>

我想要的一切:具有实际时间戳的 LabelDatum。 发生:场景出现,但初始标签文本未更改为日期

您的 FXML 文件没有指定控制器。您需要将 fx:controller 属性添加到根元素中:

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" 
  minHeight="-Infinity" minWidth="-Infinity" 
  prefHeight="478.0" prefWidth="682.0" xmlns="http://javafx.com/javafx/8" 
  xmlns:fx="http://javafx.com/fxml/1"
  fx:controller="my.package.FXMLDocumentController">

(将 my.package 替换为您的控制器 class 在其中定义的实际包)。

您需要在 SceneBuilder 中设置控制器 class(它将在 FXML 文件中设置)。

(单击图片可获得更大的分辨率、更快的加载速度和更好的质量)