【问题标题】:How to pause javafx class如何暂停 javafx 类
【发布时间】:2016-10-12 22:54:18
【问题描述】:

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

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

notify_me.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            new engine();
            Platform.exit();
        }
    });

为了重复这个过程,我使用了

Timer t = new Timer(0,new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
             while(true){
                 javafx.launch(javafx.class);
                 //some extra code goes here including sleep for
                 //some time and check for engine window state
             }
        }
    });
    t.start();

但我面临两个问题:

  1. some extra code 在平台退出之前不会实现,
  2. launch() 不能多次调用

那么我怎样才能在不使用线程的情况下实现呢?谢谢

【问题讨论】:

  • 调用 Platform.exit 会关闭应用程序并使 JavaFX 线程无效。即使理论上可以从同一个程序启动一个新的 JavaFX Application,也可能非常不鼓励这样做。你能准确解释你想要达到的目标吗?另外 - 看看this tutorialTask 可能就是你要找的。​​span>
  • 好吧,while循环在运行时会出现异常,所以我不知道为什么platform.exit没有完全终止它,所以我可以平静地重新启动它我需要的是该javafx类的暂停和播放方法终止然后重新启动它不起作用,保持它工作都不会在之后运行代码

标签: java eclipse javafx


【解决方案1】:

您可能无法使用Threads。但是,我建议不要关闭 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();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-13
    • 2021-01-20
    相关资源
    最近更新 更多