我不能将文本设置为标签 [英] I can't set text to a label

查看:63
本文介绍了我不能将文本设置为标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我想做的是单击按钮时在标签上显示文本,这给了我NullPointerException.我正在使用JavaFX.这是代码本身(我删除了一些代码是因为我认为不需要):

Basically what I want to do is to display a text on a label when a button is clicked and it gives me a NullPointerException. I am using JavaFX. Here's the code itself(I deleted some of it because I don't think it's needed):

 package controller;

import java.sql.*;

import javafx.event.*;
import javafx.fxml.FXML;
import javafx.scene.control.*;

public class LoginController {
  @FXML private TextField user;
  @FXML private PasswordField password;
  @FXML private Button loginButton;
  @FXML private Button registerButton;
  @FXML private Label errorLabel1;

  public void initialize() {}


  public void login(final WindowManager Manager) {
    loginButton.setOnAction(new EventHandler<ActionEvent>() {
      @Override public void handle(ActionEvent event) {
             user.clear();
             password.clear();
//The error is in the next line 
             errorLabel1.setText("test");
        }
      });
  }

这是单击按钮时出现的错误

And here is the error I get when I click the button

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at controller.LoginController$1.handle(LoginController.java:28)
at controller.LoginController$1.handle(LoginController.java:1)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Node.fireEvent(Unknown Source)
at javafx.scene.control.Button.fire(Unknown Source)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Scene$MouseHandler.process(Unknown Source)
at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source)
at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$350(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$175/420645852.get(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.notifyMouse(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/2117255219.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

FXML文件:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="450.0" prefWidth="330.0" styleClass="background" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.LoginController">
   <children>
      <ImageView fitHeight="128.0" fitWidth="128.0" layoutX="101.0" layoutY="36.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@images/safe-icon.png" />
         </image>
      </ImageView>
      <Label layoutX="24.0" layoutY="175.0" styleClass="label-bright" text="Welcome!">
         <font>
            <Font size="14.0" />
         </font>
      </Label>
      <Label id="usernameLabel" layoutX="24.0" layoutY="215.0" text="Username:" />
      <TextField id="user" fx:id="user" layoutX="165.0" layoutY="213.0" prefHeight="25.0" prefWidth="151.0" />
      <Label layoutX="21.0" layoutY="278.0" text="Master password:" />
      <Button fx:id="loginButton" layoutX="21.0" layoutY="364.0" mnemonicParsing="false" prefHeight="33.0" prefWidth="111.0" text="Log in." />
      <Button fx:id="registerButton" layoutX="144.0" layoutY="364.0" mnemonicParsing="false" prefHeight="33.0" prefWidth="170.0" text="New Registration" />
      <PasswordField id="password" fx:id="password" layoutX="165.0" layoutY="276.0" prefHeight="25.0" prefWidth="151.0" />
      <Label id="errorLabel1" layoutX="27.0" layoutY="322.0" textFill="RED">
         <font>
            <Font size="14.0" />
         </font>
      </Label>
   </children>
</AnchorPane>

推荐答案

标签没有 fx:id 属性.您需要

<Label id="errorLabel1" fx:id="errorLabel1" layoutX="27.0" layoutY="322.0" textFill="RED">

(根据您是否在CSS中使用它,可能不需要 id 属性).

(you may not need teh id attribute, depending on whether or not you are using it for CSS).

这篇关于我不能将文本设置为标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆