加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

从JavaFX开始:无法将javafx.scene.control.Label字段applicatio

发布时间:2020-12-15 04:22:34 所属栏目:Java 来源:网络整理
导读:我开始使用 JavaFX. 当我执行我的程序时,错误发生,在我尝试这样做之前,它工作正常并且按钮点击工作,但那是在我打算使按钮单击更改文本之前. ?import javafx.scene.text.*??import javafx.scene.control.*??import java.lang.*??import javafx.scene.layout.*
我开始使用 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,你可以看到文字是“我的文字会改变!”.在Eclipse的FXML文本编辑器中,它给了我这个错误:“你不能将’Text’分配给控制器字段’Label’”.

这是我的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

在控制器中,您需要将myLabel定义更改为

@FXML
private Text myLabel;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读