How to close a stage after a certain amount of time JavaFX

前端 未结 3 1743
时光取名叫无心
时光取名叫无心 2021-01-04 14:04

I\'m currently working with two controller classes.

In Controller1 it creates a new stage that opens on top of the main one.

Stage stage = new Stage         


        
3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-04 14:32

    Doing it your way, this would work:

    long mTime = System.currentTimeMillis();
    long end = mTime + 5000; // 5 seconds 
    
    while (mTime < end) 
    {
        mTime = System.currentTimeMilis();
    } 
    stage.close();
    

    You need to save your stage into a variable. Maybe it is better to run that in a Thread, so that you can do something within the 5 seconds. Another way would be to run a Thread.sleep(5000); and this would also be more performant than the while loop.

提交回复
热议问题