有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaFX:在很短的时间内隐藏窗格

我正在寻找一种方法,将一个窗格隐藏一小段时间(大约100毫秒),然后立即再次显示

现在,我使用的是一个顶部有两个锚的StackPane,按下键后,我移除了顶部窗格。然而,这似乎不会立即发生,而且时间太长

我还尝试使用CSS使顶部窗格不可见,但这似乎没有任何作用

下面是一些代码:

        pn_middle.setStyle("-fx-background-color: rgba(128, 128, 128, 0);");

        try {
            Thread.sleep(1000); //1 sec for testing
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        pn_middle.setStyle("-fx-background-color: rgba(128, 128, 128, 1);");

共 (1) 个答案

  1. # 1 楼答案

    使用Timer来计时要隐藏Pane的时间。试试这个例子,它包含一个StackPane,其中有一个窗格,颜色为PINK和一个Button。在click of the Button上,Pane被隐藏1000毫秒

    import java.util.Timer;
    import java.util.TimerTask;
    
    import javafx.application.Application;
    import javafx.application.Platform;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.Pane;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    
    public class HideAndShowPane extends Application {
    
        @Override
        public void start(Stage primaryStage) throws Exception {
            StackPane stackPane = new StackPane();
            Button button = new Button("Click Me to hide Pane !");
            Pane pane = new Pane();
    
            button.setOnAction(new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent event) {
                        //Hide the Pane
                        pane.setVisible(false);
                        //Schedule the Visibility for 1000ms
                        Timer timer = new Timer();
                        timer.schedule(new TimerTask() {
                              @Override
                              public void run() {
                                  //Run on UI thread
                                  Platform.runLater(new Runnable() {
                                    @Override
                                    public void run() {
                                        pane.setVisible(true);                                  
                                    }
                                });
                              }
                            }, 1000);   
                    }
            });
    
            pane.setPrefSize(200, 200);
            pane.setStyle("-fx-background-color : PINK");
            stackPane.getChildren().addAll(pane, button);
            Scene scene = new Scene(stackPane, 500, 500);
            primaryStage.setScene(scene);
            primaryStage.show();
    
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    使用任务

    您还可以通过稍后使用TaskThread.sleepad将任务的valuePropertyPanevisibleProperty绑定来实现这一点

    button.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {
                    //Create a Task
                    Task<Boolean> task = new Task<Boolean>() {
                        @Override
                        protected Boolean call() throws Exception {
                            try {
                                //Invisible for 1000ms
                                Thread.sleep(1000);
                            }
                            catch (InterruptedException e) {
                                return Boolean.FALSE;
                            }
                            return Boolean.TRUE;
                        }
                    };
                    //Start the Task
                    new Thread(task).start();
                    //Bind the visibility with Task Value
                    pane.visibleProperty().bind(task.valueProperty());
             }
    });
    

    不创建任何新线程

    多亏了Tomas Mikula's answer,这也可以在不创建任何新线程的情况下实现。使用TimelineKeyFramesKeyValue的组合

    button.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            pane.setVisible(false);
            Timeline timeline = new Timeline();
            timeline.getKeyFrames().add(
                    new KeyFrame(Duration.millis(1000), 
                            new KeyValue(pane.visibleProperty(), true)));
            timeline.play();
        }
    });