JavaFX更改标签文本

时间:2015-12-20 12:54:34

标签: java javafx fxml

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

我使用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主文件时,没有任何反应。 有没有人能够帮助我?

谢谢。 保罗

更新:

我的整个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。 发生:场景显示,但初始标签文本不会更改为日期

2 个答案:

答案 0 :(得分:1)

您的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替换为您定义的控制器类的实际包。)

答案 1 :(得分:1)

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

gfycat

(点击图片可获得更大的分辨率,更快的装载和更好的质量)

相关问题