为了账号安全,请及时绑定邮箱和手机立即绑定

显示我的网格后无法让线程休眠

显示我的网格后无法让线程休眠

杨魅力 2022-11-30 11:14:23
我有一个网格窗格,我想让它可见。然后我想暂停程序 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 后显示一毫秒。
查看完整描述

1 回答

?
慕工程0101907

TA贡献1887条经验 获得超5个赞

使用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();        


        }

}


查看完整回答
反对 回复 2022-11-30
  • 1 回答
  • 0 关注
  • 55 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信