我正在将javafx与fxml 一起用于,因此我将 Controller 用于实际编码。我需要在舞台上进行一些操作,例如获取其x轴或y轴位置。我已经尝试过stage.getX()
和stage.getY
,但是它们不起作用(阶段名称被高亮显示为错误)。如何在 Controller 中使用这些功能?我尝试在主文件中执行此操作:
public int locationX = stage.getX();
和
public double locationX = stage.getX();
但这是行不通的,反而会使整个程序成为一个大错误。
那么,如何在我的 Controller 文件中执行这些功能呢?我是否需要导入其他内容或以其他方式执行上述操作?
error: cannot find symbol
locationX = stage.getX();
symbol: variable stage
location: class FXMLController
我知道缺少“阶段”。但是,如何在我的 Controller 中获得“舞台”?
最佳答案
从fxml
文件中的根 Pane 中:
@FXML
Parent root
您可以通过以下方式从中获得阶段:
Stage stage = (Stage) root.getScene().getWindow()
您可以引用自己的舞台,可以做自己想做的事情。
关于location - 如何在JavaFX的 Controller 文件中的舞台上调用函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16636286/