使用JavaFX';s菜单栏原因';Glass检测到未完成的Java异常';

使用JavaFX';s菜单栏原因';Glass检测到未完成的Java异常';,java,javafx-2,Java,Javafx 2,我有一个JavaFX应用程序。我有一个按钮,单击该按钮时调用一个操作并加载另一个fxml文件。这很好用 我决定将此功能放在应用程序的菜单中 因此,我创建了一个菜单,并从场景生成器中添加了菜单项。我正确地分配了“On Action”事件,就像我对另一个按钮所做的那样。但是,单击时出现以下错误: Glass detected outstanding Java exception at -[GlassViewDelegate sendJavaMouseEvent:]:src/com/sun/mat/u

我有一个JavaFX应用程序。我有一个按钮,单击该按钮时调用一个操作并加载另一个fxml文件。这很好用

我决定将此功能放在应用程序的菜单中

因此,我创建了一个菜单,并从场景生成器中添加了菜单项。我正确地分配了“On Action”事件,就像我对另一个按钮所做的那样。但是,单击时出现以下错误:

Glass detected outstanding Java exception at -[GlassViewDelegate sendJavaMouseEvent:]:src/com/sun/mat/ui/GlassViewDelegate.m:541
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException

Caused by: java.lang.ClassCastException: javafx.scene.control.MenuItem cannot be cast to javafx.scene.Node
at plataformavalidezpredictiva.MainController.handleAction(MainController.java:60)
... 38 more
这是处理程序的代码。这同样适用于我在UI中放置的按钮,但不适用于菜单栏:

public void handleAction(ActionEvent event) throws Exception{
    Node node = (Node)event.getSource();
    Stage stage=(Stage) node.getScene().getWindow();

    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("fxml/GUIFile.fxml"));        

    Parent root = (Parent)fxmlLoader.load();          
    Scene scene = new Scene(root); 
    stage.setScene(scene);

    stage.show();
}    
这句话似乎给了我一个问题:

Node node = (Node)event.getSource();
有什么想法吗

编辑:我在这里看到了这篇文章,但这对我来说不起作用,因为它没有找到菜单栏的getScene()方法

将一些节点(例如
菜单栏
,但它实际上可以是同一场景中的任何节点)注入控制器。在该节点上调用
getScene()
,在场景上调用
getWindow()

例如

Main.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuItem?>

<BorderPane xmlns:fx="http://javafx.com/fxml" fx:controller="exitfrommenu.MainController">
    <top>
    <MenuBar fx:id="menuBar">
    <Menu text="File">
    <MenuItem  text="Exit" onAction="#exit"/>
    </Menu>
    </MenuBar>
    </top>
</BorderPane>
Main.java

package exitfrommenu;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.fxml.FXMLLoader;


public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            BorderPane root = (BorderPane)FXMLLoader.load(getClass().getResource("Main.fxml"));
            Scene scene = new Scene(root,400,400);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}
将一些节点(例如
菜单栏
,但它实际上可以是同一场景中的任何节点)注入控制器。在该节点上调用
getScene()
,在场景上调用
getWindow()

例如

Main.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuItem?>

<BorderPane xmlns:fx="http://javafx.com/fxml" fx:controller="exitfrommenu.MainController">
    <top>
    <MenuBar fx:id="menuBar">
    <Menu text="File">
    <MenuItem  text="Exit" onAction="#exit"/>
    </Menu>
    </MenuBar>
    </top>
</BorderPane>
Main.java

package exitfrommenu;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.fxml.FXMLLoader;


public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            BorderPane root = (BorderPane)FXMLLoader.load(getClass().getResource("Main.fxml"));
            Scene scene = new Scene(root,400,400);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}
将一些节点(例如
菜单栏
,但它实际上可以是同一场景中的任何节点)注入控制器。在该节点上调用
getScene()
,在场景上调用
getWindow()

例如

Main.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuItem?>

<BorderPane xmlns:fx="http://javafx.com/fxml" fx:controller="exitfrommenu.MainController">
    <top>
    <MenuBar fx:id="menuBar">
    <Menu text="File">
    <MenuItem  text="Exit" onAction="#exit"/>
    </Menu>
    </MenuBar>
    </top>
</BorderPane>
Main.java

package exitfrommenu;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.fxml.FXMLLoader;


public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            BorderPane root = (BorderPane)FXMLLoader.load(getClass().getResource("Main.fxml"));
            Scene scene = new Scene(root,400,400);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}
将一些节点(例如
菜单栏
,但它实际上可以是同一场景中的任何节点)注入控制器。在该节点上调用
getScene()
,在场景上调用
getWindow()

例如

Main.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuItem?>

<BorderPane xmlns:fx="http://javafx.com/fxml" fx:controller="exitfrommenu.MainController">
    <top>
    <MenuBar fx:id="menuBar">
    <Menu text="File">
    <MenuItem  text="Exit" onAction="#exit"/>
    </Menu>
    </MenuBar>
    </top>
</BorderPane>
Main.java

package exitfrommenu;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.fxml.FXMLLoader;


public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            BorderPane root = (BorderPane)FXMLLoader.load(getClass().getResource("Main.fxml"));
            Scene scene = new Scene(root,400,400);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

正如错误消息中所说,MenuItem不是节点,因此您的强制转换失败。你能解释一下“没有找到菜单栏的getScene()方法”是什么意思吗?是否尝试将菜单栏插入控制器并对其调用getScene()?该方法不存在。我没有试过那样做,我只是需要申报吗?我不知道你说的是什么意思。和。因此,该方法是存在的。我发布了一个完整的示例。正如错误消息中所说,MenuItem不是节点,因此您的强制转换失败。你能解释一下“没有找到菜单栏的getScene()方法”是什么意思吗?是否尝试将菜单栏插入控制器并对其调用getScene()?该方法不存在。我没有试过那样做,我只是需要申报吗?我不知道你说的是什么意思。和。因此,该方法是存在的。我发布了一个完整的示例。正如错误消息中所说,MenuItem不是节点,因此您的强制转换失败。你能解释一下“没有找到菜单栏的getScene()方法”是什么意思吗?是否尝试将菜单栏插入控制器并对其调用getScene()?该方法不存在。我没有试过那样做,我只是需要申报吗?我不知道你说的是什么意思。和。因此,该方法是存在的。我发布了一个完整的示例。正如错误消息中所说,MenuItem不是节点,因此您的强制转换失败。你能解释一下“没有找到菜单栏的getScene()方法”是什么意思吗?是否尝试将菜单栏插入控制器并对其调用getScene()?该方法不存在。我没有试过那样做,我只是需要申报吗?我不知道你说的是什么意思。和。因此,该方法是存在的。我贴了一个完整的例子。