簡體   English   中英

如何暫停javafx類

[英]How to pause javafx class

我正在構建一個警報,它由兩部分組成: 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()不能多次調用

那么如何在不使用線程的情況下實現呢? 謝謝

你可能不會使用Thread 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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM