如何在JavaFX中重新加载应用程序? [英] How to reload an Application in JavaFX?

查看:183
本文介绍了如何在JavaFX中重新加载应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 New Game 按钮的小游戏.按下 New Game (新游戏)按钮后,有许多变量需要重置.是否有任何方法可以轻松地重新加载整个应用程序,或者有其他方法可以刷新场景,舞台或变量?我只想在应用程序首次加载时将其带入初始阶段.

我在互联网上浏览了不同的主题,在这里也阅读了许多问题和答案,但是我没有简单的方法来实现它.大多数答案都建议重用整个代码或将整个代码放在类中并重新加载.

已审查的问题:

解决方案

我当然会建议一种干净的方法,如您所链接的问题中所述.

但是,一种快速而肮脏的方法可能是:

restartButton.setOnAction( __ ->
{
  System.out.println( "Restarting app!" );
  primaryStage.close();
  Platform.runLater( () -> new ReloadApp().start( new Stage() ) );
} );

关闭主要阶段,创建一个新阶段并将其传递给您的App的新实例.我无法对此骇客的记忆行为做任何保证.小心使用.

完整示例: https://gist.github.com/bugabinga/ce9e0ae2328ba34133bd

I have a small game which has New Game button. There are many variables which need reset when the New Game button is pressed. Is there any method which can easily reload the whole application or any alternative to refresh the scene, stage or variables? I just want to bring the application to it's initial stage when It's first loaded.

I went through different topics on internet and read many questions and answers here also, but I there is no easy method to implement it. Most of the answers suggest to reuse the whole code or put the whole code in class and reload it.

Questions reviewed:

解决方案

I would certainly recommend a clean approach as discussed in the questions you linked.

A quick and dirty way however might be the following:

restartButton.setOnAction( __ ->
{
  System.out.println( "Restarting app!" );
  primaryStage.close();
  Platform.runLater( () -> new ReloadApp().start( new Stage() ) );
} );

Close the main stage, create a new one and pass it to a new instance of your App. I cannot make any guarantees about the memory behavior of this hack. Use it carefully.

Full example: https://gist.github.com/bugabinga/ce9e0ae2328ba34133bd

这篇关于如何在JavaFX中重新加载应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆