java - 从 JavaFX : Can not set javafx. scene.control.Label 字段 application.SceneController.myLabel 开始到 javafx.scene.text.Text

标签 java javafx

我从 JavaFX 开始。

错误发生在我执行我的程序时,在我尝试这样做之前,它工作正常并且按钮点击有效,但那是在我打算让按钮点击更改文本之前。

<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.SceneController">
   <children>
      <AnchorPane layoutX="-100.0" layoutY="-224.0" prefHeight="572.0" prefWidth="430.0">
         <children>
            <Button layoutX="205.0" layoutY="253.0" mnemonicParsing="false" onAction="#handleActionButton1" text="Do you want swag?" />
            <Text fx:id="myLabel" layoutX="280.0" layoutY="339.0" strokeType="OUTSIDE" strokeWidth="0.0" text="My text will change!" />
         </children>
      </AnchorPane>
   </children>
</AnchorPane>

这就是 FXML,在线上,您可以看到文本是“My text will change!”。在 Eclipse 的 FXML 文本编辑器上它给我这个错误:“你不能将‘文本’分配给 Controller 字段‘标签’”。

这是我的 SceneController 类:

package application;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;

public class SceneController{

    @FXML
    private Label myLabel;

    @FXML
    public void handleActionButton1(ActionEvent event){
        System.out.println("Hello World!");
        myLabel.setText("Hello World!");
    }

}

正如我上面提到的,在我有一个标签之前,它会在控制台上成功输出“Hello World”,但现在它会这样做,但不会执行我的 Java 应用程序,它会给我一个很长的错误:

javafx.fxml.LoadException: 
/C:/Users/Neil/development/JavaFX/bin/application/appfxml.fxml:14

    at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.load(Unknown Source)
    at application.Main.start(Main.java:15)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(Unknown Source)
    at com.sun.javafx.application.LauncherImpl$$Lambda$51/1299755900.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$45/1051754451.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$164(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/1698159280.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$46/1775282465.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$141(Unknown Source)
    at com.sun.glass.ui.win.WinApplication$$Lambda$37/1109371569.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalArgumentException: Can not set javafx.scene.control.Label field application.SceneController.myLabel to javafx.scene.text.Text
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.set(Unknown Source)
    at java.lang.reflect.Field.set(Unknown Source)
    at javafx.fxml.FXMLLoader.injectFields(Unknown Source)

最佳答案

在您的 FXML 中,您定义了一个 Text,在您的 Controller 中,您试图将其用作 Label

stackTrace(错误)正在大喊大叫:

Can not set javafx.scene.control.Label field application.SceneController.myLabel to javafx.scene.text.Text

在您的 Controller 中,您需要将 myLabel 定义更改为

@FXML
private Text myLabel;

关于java - 从 JavaFX : Can not set javafx. scene.control.Label 字段 application.SceneController.myLabel 开始到 javafx.scene.text.Text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25967559/

相关文章:

java - 如何使用 JPA 关系实体创建 RestAPI

java - VBox(或 HBox)内的旋转对象

java - 如何在 Canvas 上绘制多个对象?

java - Tomcat 等待加入 jgroups 集群

java - AsyncTask 有错误 - 初学者

Java:如何获取短路的绝对值并保持短路

java - 将过滤后的组合框项链接到输入

JavaFX 在文本字段上按模式设置输入过滤器

Javafx,如何检测鼠标位置?

java - 基于上下文无关文法解析正则表达式