如何在JavaFX的控制器文件中调用舞台上的函数 [英] How to call functions on the stage in JavaFX's controller file
问题描述
我在和fxml 一起使用javafx,所以我使用控制器进行实际编码。我需要在舞台上做一些操作,例如获得x轴或y轴位置。我试过 stage.getX()
& stage.getY
,但它们不起作用(舞台名称因错误而被高举)。如何在控制器中使用此类功能?我尝试在我的主文件中执行此操作:
I am using javafx along with fxml, so I use the controller for the real coding. I need to do a few operations on the stage, such as getting its x- or y-axis position. I have tried stage.getX()
& stage.getY
, but they don't work(the stage name is high-lited as the error). How do I use such functions in my controller? I tried doing this in my main file:
public int locationX = stage.getX();
和
public double locationX = stage.getX();
但它不起作用,反而使整个程序出现一个大错误。
那么如何在我的控制器文件中执行此类功能?我是否需要以其他方式导入某些内容或执行上述操作?
But it doesn't work, instead makes the whole program one big error.
So how do I get to do such functions in my controller file? Do I need to import something or do something like above in another way?
error: cannot find symbol
locationX = stage.getX();
symbol: variable stage
location: class FXMLController
我知道缺少舞台。但是如何在我的控制器中获得阶段?
推荐答案
从 root fxml
文件中的窗格:
@FXML
Parent root
您可以通过以下方式获得舞台:
You can get the stage from it by:
Stage stage = (Stage) root.getScene().getWindow()
你可以参考你的舞台,你可以做你想做的事。
You have a reference to your stage, you can do what you want.
这篇关于如何在JavaFX的控制器文件中调用舞台上的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!