2016-11-26 90 views
1

有沒有辦法知道AnimationTimer是否在運行?我想切換這樣的暫停菜單:如何知道AnimationTimer是否正在運行?

setOnKeyTyped(event -> { 
    switch (event.getCode()) { 
    case SPACE: 
     if (animationTimer.isRunning()) { // What should I put here? 
     animationTimer.stop(); 
     } 
     else { 
     animationTimer.start(); 
     } 
     break; 

    default: 
     break; 
    } 
} 
+0

對我來說,這似乎是一個非常糟糕的設計... – GOXR3PLUS

+0

我聽任何建議。 – SteeveDroz

+1

這裏是一個建議http://stackoverflow.com/questions/41205977/javafx-why-animationtimer-has-not-getstatus-or-similar-method/41206746#41206746。 – GOXR3PLUS

回答

1

您可以覆蓋startstop,以保持其信息的計時器是否正在運行與否:

class StatusTimer extends AnimationTimer { 

    private volatile boolean running; 

    @Override 
    public void start() { 
     super.start(); 
     running = true; 
    } 

    @Override 
    public void stop() { 
     super.stop(); 
     running = false; 
    } 

    public boolean isRunning() { 
     return running; 
    } 

    ... 

} 

似乎是沒有其他辦法無需訪問AnimationTimer的私人成員。

+0

['AnimationTimer'](https://docs.oracle.com/javafx/2/api/javafx/animation/AnimationTimer.html)沒有'getStatus()'方法。 – SteeveDroz

相關問題