这是控制器的代码
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>当我运行程序时,clockLabel保持“时钟”不变。它也不会给我任何错误。当我在稍后创建的构造函数中放置nullPointerException ()时,它给了我一个nullPointerException错误
我试着在this.clockLabel.setText(dateFormat.format(currentDate));中摆脱这个问题,但也没有奏效。
发布于 2020-03-07 10:02:01
只有当initialize方法从Initializable重写该方法时,才会考虑接受参数的方法。在您的示例中,控制器类不实现Initializable,因此FXMLLoader只查找initialize()方法。要么删除方法的参数,要么将implements Initializable添加到类中。
至于导致NPE的构造函数:字段的注入是在构造函数完成控制器生命周期之后发生的:
FXMLLoader.load是enteredfx:controller属性,并使用反射创建controller.fx:idsFXMLLoader.load返回。发布于 2020-03-07 10:15:36
除了Fabian的答案,您还可以使用@FXML注释来确保使用正确的方法:
@FXML
private void initialize() {
...
}https://stackoverflow.com/questions/60576209
复制相似问题