JavaFX无法在MenuItem上使用onAction

JavaFX无法在MenuItem上使用onAction,java,javafx,scenebuilder,Java,Javafx,Scenebuilder,当按下QuitMenuItem时,我正在尝试创建一种退出应用程序的方法 我有以下方法: @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 Namesp

当按下
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
符号,该方法未定义为
静态
,因此它应该可以工作,并且我已正确导入
操作事件
Edit1:好的,我成功地将
场景生成器
脚本模式
切换到
方法模式
,这解决了问题,但现在我得到:

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

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

onAction接受接受ActionEvent作为参数的方法确保提供的方法以ActionEvent作为参数

范例

FXML

<Menu mnemonicParsing="false" text="File">
   <items>
       <MenuItem mnemonicParsing="false" fx:id="action_backup" text="Backup" onAction="#performAction"/>
       <MenuItem mnemonicParsing="false" fx:id="action_settings" text="Settings" onAction="#performAction"/>
   </items>
</Menu>

您的fxml文件中有fx:controller属性吗?没有,但我不明白为什么现在需要它?我试图将它设置为crasname:Player,但它不起作用,找不到它。需要解决“#doExit”,否则它甚至猜不到在哪里查找它;)。为播放器提供完整的包路径,比如fx:controller=“path.to.my.Player”我设法解决了这个问题,我通过编程创建了控制器。太棒了!您可以在下面的回答中以博客条目的形式详细说明您的发现和“经验法则”。如果您向根元素添加属性,还可以在fxml文件中指定控制器(请参阅)。在这种情况下,
fxmloader
将为您创建控制器(如果默认构造函数为public)。。既然你没有保留对控制器的引用,那也没关系。。。不要同时使用这两种可能性,否则会出现另一个错误。
<Menu mnemonicParsing="false" text="File">
   <items>
       <MenuItem mnemonicParsing="false" fx:id="action_backup" text="Backup" onAction="#performAction"/>
       <MenuItem mnemonicParsing="false" fx:id="action_settings" text="Settings" onAction="#performAction"/>
   </items>
</Menu>
public void performAction(ActionEvent actionEvent) {
    //you can add this method for multiple menu item and identify
    //each menu item by its id
   MenuItem target  = (MenuItem) actionEvent.getSource();
   System.out.println("Clicked On Item:"+target.getId()); 
}