簡體   English   中英

從JavaFX開始:無法將javafx.scene.control.Label字段application.SceneController.myLabel設置為javafx.scene.text.Text

[英]Starting off with JavaFX : Can not set javafx.scene.control.Label field application.SceneController.myLabel to javafx.scene.text.Text

我開始使用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(錯誤)在它的聲音頂部喊它:

無法將javafx.scene.control.Label字段application.SceneController.myLabel設置為javafx.scene.text.Text

在控制器中,您需要將myLabel定義更改為

@FXML
private Text myLabel;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM