javafx中的标签不会更改

javafx中的标签不会更改,javafx,fxml,scenebuilder,Javafx,Fxml,Scenebuilder,这是控制器的代码 package Views; import java.io.File; import java.io.IOException; import java.net.URL; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import

这是控制器的代码

package Views;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.ResourceBundle;

import javax.imageio.ImageIO;

import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;

public class menuController {

    @FXML
    private Label eNameLabel;
    @FXML
    private Button productListButton;
    @FXML
    private Button employeeListButton;
    @FXML
    private Button ingredientListButton;
    @FXML
    private Button addProductButton;
    @FXML
    private Button addIngreidentButton;
    @FXML
    private Button addEmployeeButton;
    @FXML
    private Button LogOutButton;
    @FXML
    private Label clockLabel;



    public void initialize(URL arg0, ResourceBundle arg1) {
        clock();
    }

    public void clock(){
        Calendar cal = new GregorianCalendar();
    //  int day= cal.get(Calendar.DAY_OF_MONTH);
    //  int month = cal.get(Calendar.MONTH);
    //  int year = cal.get(Calendar.YEAR);

        Date currentDate = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");

    //  int second = cal.get(Calendar.SECOND);
    //  int minute = cal.get(Calendar.MINUTE);
    //  int hour = cal.get(Calendar.HOUR);

        this.clockLabel.setText(dateFormat.format(currentDate));
    }
}
启动程序时,我的标签不会更改。 我已经检查了场景生成器中是否提供了标签id,并且所有内容都已连接到其控制器或变量

这是连接到控制器的fxml文件

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.text.Font?>

<AnchorPane prefHeight="764.0" prefWidth="901.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Views.menuController">
   <children>
      <AnchorPane layoutY="2.0" prefHeight="764.0" prefWidth="951.0">
         <children>
            <HBox prefHeight="100.0" prefWidth="746.0" spacing="20.0">
               <children>
                  <Label lineSpacing="10.0" text="Welcom!">
                     <font>
                        <Font size="55.0" />
                     </font>
                  </Label>
                  <Label fx:id="eNameLabel" lineSpacing="10.0" prefHeight="81.0" prefWidth="244.0" text="Label">
                     <font>
                        <Font size="55.0" />
                     </font>
                  </Label>
               </children>
            </HBox>
            <Button fx:id="LogOutButton" layoutX="768.0" layoutY="704.0" mnemonicParsing="false" onAction="#logOutButtonPushed" prefHeight="46.0" prefWidth="176.0" text="Log Out" />
            <Button fx:id="employeeListButton" layoutX="74.0" layoutY="301.0" mnemonicParsing="false" prefHeight="81.0" prefWidth="215.0" text="Employee List" />
            <Button fx:id="productListButton" layoutX="368.0" layoutY="301.0" mnemonicParsing="false" prefHeight="81.0" prefWidth="215.0" text="Products List" />
            <Button fx:id="ingredientListButton" layoutX="661.0" layoutY="301.0" mnemonicParsing="false" prefHeight="81.0" prefWidth="215.0" text="Ingredients List" />
            <Label fx:id="clockLabel" layoutX="74.0" layoutY="710.0" text="Clock" />
            <Button fx:id="addEmployeeButton" layoutX="74.0" layoutY="439.0" mnemonicParsing="false" prefHeight="81.0" prefWidth="215.0" text="Add Employee" />
            <Button fx:id="addProductButton" layoutX="368.0" layoutY="439.0" mnemonicParsing="false" prefHeight="81.0" prefWidth="215.0" text="Add Products" />
            <Button fx:id="addIngreidentButton" layoutX="661.0" layoutY="439.0" mnemonicParsing="false" prefHeight="81.0" prefWidth="215.0" text="Add Ingredients" />
         </children>
      </AnchorPane>
   </children>
</AnchorPane>
当我运行程序时,时钟标签保持“时钟”不变。 它也没有给我任何错误。 当我把时钟;在我稍后创建的构造函数中,它在this.clockLabel.setTextdateFormat.formatcurrentDate处给了我一个nullPointerException错误

我试着在this.clockLabel.setTextdateFormat.formatcurrentDate中摆脱它;但是它也不起作用。

只有当它从Initializable重写方法时,才会考虑使用参数的initialize方法。在您的情况下,控制器类不实现Initializable,因此FXMLLoader只查找initialize方法。删除方法的参数或将可初始化的实现添加到类中

对于导致NPE的构造函数:字段的注入发生在构造函数在控制器生命周期中完成之后:

输入FXMLLoader.load 解析fxml加载程序遇到fx:controller属性并使用反射创建控制器实例。 根据字段的fx:id注入字段 FXMLLoader.load返回。 只有在从Initializable重写方法时,才会考虑使用参数的initialize方法。在您的情况下,控制器类不实现Initializable,因此FXMLLoader只查找initialize方法。删除方法的参数或将可初始化的实现添加到类中

对于导致NPE的构造函数:字段的注入发生在构造函数在控制器生命周期中完成之后:

输入FXMLLoader.load 解析fxml加载程序遇到fx:controller属性并使用反射创建控制器实例。 根据字段的fx:id注入字段 FXMLLoader.load返回。
除了Fabian的答案之外,您还可以使用@FXML注释来确保使用了正确的方法:

@FXML
private void initialize() {
    ...
}

除了Fabian的答案之外,您还可以使用@FXML注释来确保使用了正确的方法:

@FXML
private void initialize() {
    ...
}

您发布的代码无法编译。什么是课堂场景?请阅读SceneChange部分对我遇到的问题没有影响。它可以忽略,因为它只允许按钮将用户指向另一个页面。您发布的代码不会编译。什么是课堂场景?请阅读SceneChange部分对我遇到的问题没有影响。它可以忽略,因为它只允许一个按钮将用户引导到另一个页面。