[英]How do I call an object an object from a .fxml file in my controller file?
I am making a stopwatch program and I want to set a photo for rotation. 我正在制作秒表程序,我想设置旋转照片。 The problem is, is that I dont know how to call the photo in the controller file. 问题是,我不知道如何在控制器文件中调用照片。 It just tells me that it cannot find symbol. 它只是告诉我找不到符号。
Heres the photo I want to call from my .fxml file 这是我想从我的.fxml文件中调用的照片
<ImageView fx:id="hand">
<image>
<Image url="@hand.png"/>
</image>
</ImageView>
And heres where I call it in my controller file, but I get an error. 在我的控制器文件中将其命名为Heres,但出现错误。
@FXML
private void updateStopwatch(){
elapsedTime++;
Integer rotation = elapsedTime * 6;
hand.setRotate(rotation);
}
Thanks for the help in advance. 我在这里先向您的帮助表示感谢。 The error I get is the following : 我得到的错误如下:
hand.setRotate(rotation);
^^^^
Cannot find symbol hand
When you have given the fxml object you want to inject into your controller the correct property fx:id="hand"
you need to declare that id as a property in your controller class. 给定fxml对象后,要将其正确的属性fx:id="hand"
注入到控制器中,您需要将该id声明为控制器类中的属性。
public class MyController {
@FXML private ImageView hand;
private void updateStopwatch(){
elapsedTime++;
Integer rotation = elapsedTime * 6;
hand.setRotate(rotation);
}
}
In your fxml file you must refer to the controller(MyController) by adding this attribute in root element: 在fxml文件中,必须通过在根元素中添加以下属性来引用controller(MyController):
fx:controller="some.package.MyController"
your fxml file may only refer to one controller. 您的fxml文件只能引用一个控制器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.