【问题标题】:Can't get the thread to sleep after displaying my grid显示我的网格后无法让线程进入睡眠状态
【发布时间】:2019-05-15 18:09:39
【问题描述】:

我有一个网格窗格,我想让它可见。然后我想暂停程序 2 秒钟,让网格再次不可见。由于某种原因,在我在程序中使用的 thread.sleep 之后网格变得可见。

这一切都发生在按钮点击事件中。

我尝试在 thread.sleep 中移动,将它们放入一个新方法并使用多个睡眠,但没有任何效果。

gameGrid.setVisible(true) 
gameGrid.setVisible(false) 

按钮事件:

public void handleButtonGo(ActionEvent Event) throws IOException {    //On go button press

        boolean validation = true;
        try {

            gameGrid.setVisible(true);
            placeShips();        

        }catch (Exception e){

            labelwarning.setText(e.getMessage());   //on error the program will stop trying to place ships and refresh any ships placed so far.
            validation = false;
            //gameGrid.getChildren().clear();
            //BoardSetup();
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            gameGrid.setVisible(false);

        }
}

网格在 thread.sleep 之后显示一毫秒。

【问题讨论】:

  • 查看 JavaFX 动画类。很多有用的东西。更具体地说是 PauseTransition。
  • 了解TimeLinePauseTransitionstackoverflow.com/questions/9966136/…。在PauseTransition 示例中,您不需要wait.playFromStart();

标签: java eclipse events javafx


【解决方案1】:

使用PauseTransition

public void handleButtonGo(ActionEvent Event) throws IOException {    //On go button press

        boolean validation = true;
        try {

            gameGrid.setVisible(true);
            placeShips();        

        }catch (Exception e){

            labelwarning.setText(e.getMessage());   //on error the program will stop trying to place ships and refresh any ships placed so far.
            validation = false;
            //gameGrid.getChildren().clear();
            //BoardSetup();
            PauseTransition wait = new PauseTransition(Duration.seconds(2));
            wait.setOnFinished((e) -> {
                /*YOUR METHOD*/      
                gameGrid.setVisible(false);
            });
            wait.play();        

        }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-23
    • 2021-06-05
    • 2011-05-12
    相关资源
    最近更新 更多