首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何重启javafx应用程序?

如何重启javafx应用程序?
EN

Stack Overflow用户
提问于 2021-02-22 14:59:12
回答 2查看 199关注 0票数 0

以下是不使用fxml重新启动JavaFX应用程序的代码。

代码语言:javascript
复制
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;


public class HelloFX extends Application
{
boolean state = true;

@Override
public void start( final Stage primaryStage )
{
    System.out.println( "state is " + state );
    playGame();
    System.out.println( "state is " + state );

    final Button restartButton = new Button( "Restart" );
    restartButton.setOnAction( __ ->
    {
        System.out.println( "Restarting app!" );
        primaryStage.close();
        Platform.runLater( () -> new HelloFX().start( new Stage() ) );
    } );
    primaryStage.setScene( new Scene( new StackPane( restartButton ) ) );
    primaryStage.show();
}

/**
 * Simulate a game play by changing the global state.
 */
private void playGame()
{
    state = false;
}

/**
 * @param args ignored.
 */
public static void main( final String[] args )
{
    launch( args );
}
}

但是,我想用fxml来做这件事。

我不知道如何在fxml中应用这段代码。

我的代码。

Main.java

代码语言:javascript
复制
package sample;

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

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{
    Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
    primaryStage.setTitle("Hello World");
    primaryStage.setScene(new Scene(root, 300, 275));
    primaryStage.show();
}


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

Controller.java

代码语言:javascript
复制
package sample;

public class Controller {

}

sample.fxml

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>


<GridPane alignment="center" hgap="10" vgap="10" xmlns:fx="http://javafx.com/fxml/1" 
xmlns="http://javafx.com/javafx/11.0.1" fx:controller="sample.Controller">
<children>
  <Button mnemonicParsing="false" onAction="#restart" text="restart" />
</children>
</GridPane>

我想重新启动我的JavaFX应用程序。

我不知道如何在fxml中应用这段代码。

如何使用fxml重新启动JavaFX应用程序?

EN

回答 2

Stack Overflow用户

发布于 2021-02-22 20:54:28

你可以试试这个:

代码语言:javascript
复制
public void restartApplication()
{
  final File currentFile = new File(TheCurrentClass.class.getProtectionDomain().getCodeSource().getLocation().toURI());

  /* is it a jar file? */
  if(!currentJar.getName().endsWith(".jar"))
    return;

  /* Build command: java -jar application.jar */
  final String command = "java -jar " + currentJar.getPath();

  final ProcessBuilder builder = new ProcessBuilder(command);
  builder.start();
  System.exit(0);
}

这基本上是通过java -jar命令重新启动程序

票数 0
EN

Stack Overflow用户

发布于 2021-02-22 21:11:17

您需要将FXML中指定的事件处理程序与控制器类绑定,例如,将restart函数添加到控制器,其代码与之前的处理程序类似:

代码语言:javascript
复制
public void restart(Event e){
    System.out.println("restart clicked");

    Button button = (Button)e.getSource();
    Stage stage = (Stage) button.getScene().getWindow();

    Platform.runLater( () -> {
        try
        {
            stage.close();
            new Main().start( new Stage() );
        }
        catch (Exception e1)
        {
            // TODO:
            e1.printStackTrace();
        }
    } );
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66311265

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档