gpt4 book ai didi

JavaFX 无法在 MenuItem 上使用 onAction

转载 作者:行者123 更新时间:2023-11-30 07:46:10 25 4
gpt4 key购买 nike

我正在尝试创建一种方法,以便在按下 Quit MenuItem退出应用程序。

我有以下方法:

@FXML
public void doExit(ActionEvent event) {
Platform.exit();
System.exit(0);
}

我收到此错误:

javafx.fxml.LoadException: Error resolving onAction='doExit', either the event handler is not in the Namespace or there is an error in the script.
project/build/resources/main/Player.fxml:21

Player.fxml 的第 21 行是这样的:

        <MenuItem mnemonicParsing="false" onAction="doExit" text="Quit" />

我尝试删除/添加 @FXML 表示法,该方法未定义为 static 所以它应该可以工作,并且我正确导入了 ActionEvent

最佳答案

Edit1:好的,我设法在场景生成器中从脚本模式切换到方法模式,这解决了问题,但现在我是得到:

javafx.fxml.LoadException: No controller specified.
project/build/resources/main/Player.fxml:21

<MenuItem mnemonicParsing="false" onAction="#doExit" text="Quit" />

编辑2:我设法以编程方式创建 Controller ,因为我没有任何包声明,而且我不想创建一个。

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Player.fxml"));
fxmlLoader.setController(new Player());
Parent root = (Parent)fxmlLoader.load();

关于JavaFX 无法在 MenuItem 上使用 onAction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33933003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com