首页javaapplicationJavaFX - 如何处理JavaFX应用程序生命周期方法

JavaFX - 如何处理JavaFX应用程序生命周期方法

我们想知道如何处理JavaFX应用程序生命周期方法。
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;

public class Main extends Application {
  public FXLifeCycleApp() {
    String name = Thread.currentThread().getName();
  }

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

  @Override
  public void init() {
    String name = Thread.currentThread().getName();
    System.out.println("init() method: " + name);
  }

  @Override
  public void start(Stage stage) {
    String name = Thread.currentThread().getName();
    System.out.println("start() method: " + name);
    
    // Add an Exit button to the scene
    Button exitBtn = new Button("Exit");
    exitBtn.setOnAction(e -> Platform.exit());
    
    Scene scene = new Scene(new Group(exitBtn), 300, 100);
    stage.setScene(scene);
    stage.setTitle("JavaFX Application Life Cycle");
    stage.show();
  }

  @Override
  public void stop() {
    String name = Thread.currentThread().getName();
    System.out.println("stop() method: " + name);
  }
}