【发布时间】: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