【问题标题】:How Can I Call Stage.initOwner() From a MenuItem On Action Method?如何从 MenuItem On Action 方法调用 Stage.initOwner()?
【发布时间】:2014-05-25 03:52:28
【问题描述】:

我刚从Is There any Way to Create A Child Window That Has the Same Properties as the Parent? 来,我试图打电话给stage.initOwner(((Node) event.getSource()).getScene().getWindow());,但没有成功,我遇到了异常。我认为我没有正确使用它。我应该如何尝试从 onAction 方法调用它?

原始代码块:

@FXML
private void onNewClanCreation(ActionEvent event) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("ClanCreationPanel.fxml"));
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.initOwner(((Node) event.getSource()).getScene().getWindow());
stage.setResizable(false);
stage.setTitle("New Clan Creation");
stage.show();
}

例外:

`Caused by: java.lang.ClassCastException: javafx.scene.control.MenuItem cannot be cast to javafx.scene.Node at com.gmail.physicistsarah.program.core.ControlPanelController.onNewClanCreation(ControlPanelController.java:65)
... 54 more`

编辑:添加 stage.initOwner

【问题讨论】:

  • 嗨,莎拉,请发布您遇到的异常。提供的代码片段也不包含 stage.initOwner ?
  • @Jurgen 刚刚进行了编辑。
  • 错误的问题在于您的堆栈跟踪,您可以将MenuItem 转换为Node,因为它不是从相同的扩展而来。您需要Node 才能从中获取Window
  • Itachi 的意思是:你不能将 MenuItem 转换为 Node ....

标签: java user-interface javafx


【解决方案1】:

这行得通: stage.initOwner(this.nameLabel.getScene().getWindow()); 谢谢@ItachiUchiha

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-24
    • 1970-01-01
    • 2016-02-29
    • 2013-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多