java 从 JavaFX 开始:无法将 javafx.scene.control.Label 字段 application.SceneController.myLabel 设置为 javafx.scene.text.Text
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25967559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Starting off with JavaFX : Can not set javafx.scene.control.Label field application.SceneController.myLabel to javafx.scene.text.Text
提问by The Bat
I'm starting off with JavaFX.
我从 JavaFX 开始。
The error occurs when I execute my program, before I attempted to do this, it worked fine and button clicks worked, but that was before it was my intention to make the button click change the text.
当我执行我的程序时发生错误,在我尝试执行此操作之前,它工作正常并且按钮点击工作,但那是在我打算让按钮点击更改文本之前。
<?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>
That's the FXML, on the line, you can see the text is "My text will change!". On Eclipse's FXML text editor it gives me this error : "You can not assign 'Text' to the controller field 'Label'".
那是FXML,上线了,你可以看到文字是“我的文字会改变!”。在 Eclipse 的 FXML 文本编辑器上,它给了我这个错误:“你不能将‘文本’分配给控制器字段‘标签’”。
This is my SceneController class:
这是我的 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!");
}
}
As I mentioned above, before I had a label, it would successfully output "Hello World" on the console, but now it does that, but doesn't execute my Java application and it gives me a long error:
正如我上面提到的,在我有一个标签之前,它会在控制台上成功输出“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$launchApplication13(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda/1299755900.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait6(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda/1051754451.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null4(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda/1698159280.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater5(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda/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$null1(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda/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)
回答by ItachiUchiha
Inside your FXML
, you have defined a Text
, where as in your Controller, you are trying to use it as Label
.
在您的 中FXML
,您已经定义了一个Text
,而在您的控制器中,您正试图将其用作Label
。
The stackTrace (error) is shouting it on the top of its voice :
stackTrace(错误)正在大声喊叫:
Can not set javafx.scene.control.Labelfield application.SceneController.myLabel to javafx.scene.text.Text
无法将javafx.scene.control.Label字段 application.SceneController.myLabel设置为javafx.scene.text.Text
In your controller, you need to change the myLabel
definition to
在您的控制器中,您需要将myLabel
定义更改为
@FXML
private Text myLabel;