我正在嘗試爲我的應用程序製作用Java編寫的GUI。JavaFX更改標籤文本
我使用Scene Builder製作了fxml文檔,正確設置了fx:id,現在我試圖在窗體中進行簡單的更改。
我DocumentController:
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主文件,沒有任何反應。 有沒有人能幫我?
謝謝。 保羅
UPDATE:
我的整個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實際時間戳。 發生:場景顯示,但初始標籤文本不會更改爲日期