如何在关闭阶段 JavaFX 后停止 WebEngine?

use*_*552 4 java youtube javafx stage javafx-webengine

当我使用 WebEngine 创建新舞台来播放 YouTube 视频时,在我关闭它之后 - Youtube 继续在后台播放。如果我使用“Platform.exit” - 它会关闭我所有的JavaFX应用程序,但我只想关闭为YouTube创建的阶段。

这是我为 YouTube 播放器设计的课程:

public class YouTube_player  {
    public YouTube_player(String url) {
        final Group root = new Group();
        Scene scene = new Scene(root, 820, 480);

        final Stage stage = new Stage();
        final WebView webView = new WebView();
        final WebEngine webEngine = webView.getEngine();
        webEngine.loadContent(url);
        root.getChildren().add(webView);
        stage.centerOnScreen();

        stage.setScene(scene);
        stage.show();
        stage.setOnCloseRequest(new EventHandler<WindowEvent>(){

            @Override
            public void handle(WindowEvent event) {
              //What i should put here to close only this stage.
              //Platform.exit - closes all my stages.
              //webEngine.getLoadWorker().cancel(); - dont stop Youtube )))
            }
        });

    }
}
Run Code Online (Sandbox Code Playgroud)

在我点击“mainstage”中的按钮后,我的 Youtube 播放器舞台正在创建:

b1.setOnAction(new EventHandler<ActionEvent>() {
    @Override public void handle(ActionEvent event) {
        new YouTube_player(url_video_p1);
    }
});
Run Code Online (Sandbox Code Playgroud)

Ita*_*iha 5

你不能处置 webengine,你唯一能做的就是将 webEngine 的内容设置为null

webView.getEngine().load(null);