如何暂停javafx类

如何暂停javafx类,java,eclipse,javafx,Java,Eclipse,Javafx,我正在制作一个报警器,它由两部分组成 在javafx类中创建的动画按钮和正常创建的引擎 我需要的是,每当用户按下关闭按钮的动画按钮并启动发动机时,发动机关闭后会有一段时间,然后动画按钮再次出现,依此类推 所以我用了: notify_me.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { n

我正在制作一个报警器,它由两部分组成 在
javafx
类中创建的动画按钮和正常创建的引擎

我需要的是,每当用户按下关闭按钮的动画按钮并启动发动机时,发动机关闭后会有一段时间,然后动画按钮再次出现,依此类推 所以我用了:

notify_me.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            new engine();
            Platform.exit();
        }
    });
但我面临两个问题:

  • 一些额外的代码
    在平台退出之前不会执行
  • 不能多次调用
    launch()

  • 那么,我如何在不使用线程的情况下实现这一点呢??谢谢

    您可能无法使用
    线程
    s。但是,我建议不要关闭fx应用程序线程。只需关闭所有窗口并在延迟后再次显示(部分):

    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button("Hide me 5 sec");
    
        // prevent automatic exit of application when last window is closed
        Platform.setImplicitExit(false);
    
        StackPane root = new StackPane();
        root.getChildren().add(btn);
    
        Scene scene = new Scene(root);
    
        primaryStage.setScene(scene);
    
        // timer should be a daemon (-> not prevent jvm shutdown)
        Timer timer = new Timer(true);
    
        btn.setOnAction((ActionEvent event) -> {
    
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    // make window reappear (needs to happen on the application thread)
                    Platform.runLater(primaryStage::show);
                }
            }, 5000l);
    
            // hide window
            primaryStage.close();
        });
    
        // allow exiting the application by clicking the X
        primaryStage.setOnCloseRequest(evt -> Platform.exit());
    
        primaryStage.show();
    }
    

    调用
    Platform.exit
    关闭应用程序并使JavaFX线程无效。即使理论上可以从同一个程序启动一个新的JavaFX
    应用程序
    ,也可能会非常不鼓励。你能准确地解释一下你想要达到的目标吗?还有-看看<代码>任务可能就是你想要的。虽然循环在运行时会出现异常,所以我不知道为什么platform.exit不能完全终止它,所以我可以和平地重新启动它。我需要的是一个暂停播放方法,用于javafx类,因为终止后重新启动它不起作用,这两种方法都不会让它继续工作,而是在以后运行代码
    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button("Hide me 5 sec");
    
        // prevent automatic exit of application when last window is closed
        Platform.setImplicitExit(false);
    
        StackPane root = new StackPane();
        root.getChildren().add(btn);
    
        Scene scene = new Scene(root);
    
        primaryStage.setScene(scene);
    
        // timer should be a daemon (-> not prevent jvm shutdown)
        Timer timer = new Timer(true);
    
        btn.setOnAction((ActionEvent event) -> {
    
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    // make window reappear (needs to happen on the application thread)
                    Platform.runLater(primaryStage::show);
                }
            }, 5000l);
    
            // hide window
            primaryStage.close();
        });
    
        // allow exiting the application by clicking the X
        primaryStage.setOnCloseRequest(evt -> Platform.exit());
    
        primaryStage.show();
    }