JavaFX Running a large amount of countdown timers at once?

前端 未结 4 1123
面向向阳花
面向向阳花 2021-01-28 10:04

So I can see a couple different ways to do what I need and I\'ve done a bunch of google/stack overflow searching but can\'t find really what I\'m looking for. I need to run mult

4条回答
  •  遥遥无期
    2021-01-28 10:34

    Here is another way, standard java. Depending on how long the countdown runs one may want to stop those executors when the GUI is closed. I am using the ScheduledExecutorService also for multiple countdowns.

    import java.time.LocalTime;
    import java.time.format.DateTimeFormatter;
    import java.util.concurrent.Executors;
    import java.util.concurrent.ScheduledExecutorService;
    import java.util.concurrent.TimeUnit;
    import java.util.concurrent.atomic.AtomicInteger;
    
    import javafx.application.Application;
    import javafx.application.Platform;
    import javafx.geometry.Insets;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.layout.HBox;
    import javafx.stage.Stage;
    
    public class CountDownExecutor extends Application{
    
        private final int i= 15;
        private final DateTimeFormatter HH_MM_SS = DateTimeFormatter.ofPattern("HH:mm:ss");
        private final Label l1=new Label("00:00:00");
        private final Insets insets = new Insets(3,5,3,5);
        private final Button button = new Button("Start");
    
        private ScheduledExecutorService executor=null;
        private AtomicInteger atomicInteger = new AtomicInteger();
    
        public static void main(String[] args) {
            Application.launch(CountDownExecutor.class, args);
        }
    
        @Override
        public void start(Stage stage) {
            HBox hb = new HBox();
            button.setOnMouseClicked(a-> countDown());
            button.setPadding(insets);
            l1.setPadding(insets);
            hb.getChildren().addAll(button,l1);
            Scene scene = new Scene(hb);
            stage.setOnCloseRequest((ev)-> {if(executor!=null) executor.shutdownNow();});
            stage.setScene(scene);
            stage.show();
        }
    
        public void countDown() {
            Platform.runLater( () -> button.setDisable(true));
            atomicInteger.set(i);
            setCountDown(LocalTime.ofSecondOfDay(atomicInteger.get()));
            executor = Executors.newScheduledThreadPool(1);
    
            Runnable r = ()->{
                int j = atomicInteger.decrementAndGet();
                if(j<1 ){
                    executor.shutdown();
                    Platform.runLater( () ->{ 
                        button.setDisable(false);
                    });
                    setCountDown(LocalTime.ofSecondOfDay(0));
                }else {
                    setCountDown(LocalTime.ofSecondOfDay(j));
                }
            };
            executor.scheduleAtFixedRate(r, 1, 1, TimeUnit.SECONDS);
        }
    
        public void setCountDown(LocalTime lt)  { Platform.runLater(() -> l1.setText(lt.format(HH_MM_SS))); }
    } 
    

提交回复
热议问题