【问题标题】:Label in javafx does not changejavafx中的标签不会改变
【发布时间】:2020-03-07 09:38:14
【问题描述】:

这是控制器的代码

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 错误 this.clockLabel.setText(dateFormat.format(currentDate));

我尝试在 this.clockLabel.setText(dateFormat.format(currentDate));但它也没有工作。

【问题讨论】:

  • 您发布的代码无法编译。 SceneChanger 是什么类?请阅读How to create a Minimal, Reproducible Example
  • Scenechanger 部分对我遇到的问题没有影响。可以忽略它,因为它只允许一个按钮将用户引导到另一个页面。

标签: javafx fxml scenebuilder


【解决方案1】:

只有在覆盖Initializable 中的方法时,才会考虑采用参数的initialize 方法。在您的情况下,控制器类没有实现Initializable,因此FXMLLoader 只查找initialize() 方法。要么删除方法的参数,要么将implements Initializable 添加到类中。

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

  1. FXMLLoader.load 已输入
  2. 加载器在解析 fxml 时遇到fx:controller 属性并使用反射来创建控制器的实例。
  3. 根据fx:ids 注入字段
  4. FXMLLoader.load 返回。

【讨论】:

    【解决方案2】:

    除了Fabian的回答——你还可以使用@FXML注解来确保使用了正确的方法:

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

    【讨论】:

      猜你喜欢
      • 2020-04-27
      • 1970-01-01
      • 2015-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多