JavaFX:在很短的时间内隐藏窗格
我正在寻找一种方法,将窗格隐藏一小段时间(大约100毫秒),然后立即再次显示 现在我使用的是一个顶部有两个锚的StackPane,按下键我就移除了顶部窗格。然而,这似乎并没有立即发生,而且时间太长了 我还尝试使用CSS使顶部窗格不可见,但这似乎没有任何作用 下面是一些代码:JavaFX:在很短的时间内隐藏窗格,java,javafx,visibility,Java,Javafx,Visibility,我正在寻找一种方法,将窗格隐藏一小段时间(大约100毫秒),然后立即再次显示 现在我使用的是一个顶部有两个锚的StackPane,按下键我就移除了顶部窗格。然而,这似乎并没有立即发生,而且时间太长了 我还尝试使用CSS使顶部窗格不可见,但这似乎没有任何作用 下面是一些代码: pn_middle.setStyle("-fx-background-color: rgba(128, 128, 128, 0);"); try { Thread.s
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);");
使用
计时器
来计时要隐藏窗格的时间
。试试这个例子,它包含一个堆栈窗格
,其中有一个窗格,颜色为粉红色
,还有一个按钮
。单击按钮,窗格
隐藏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);
}
}
不创建任何新线程
由于,这也可以在不创建任何新线程的情况下实现。使用时间线
、关键帧
和关键值
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();
}
});
button.setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
窗格。设置为可见(假);
时间线=新时间线();
timeline.getKeyFrames().add(
新关键帧(持续时间.毫秒(1000),
新的键值(pane.visibleProperty(),true));
timeline.play();
}
});
如果您使用JavaFX8,下面是一个使用来自的计时器的解决方案。与@ItachiUchiha的解决方案不同,它不会创建任何新线程
import java.time.Duration;
import org.reactfx.util.FxTimer;
button.setOnAction(event -> {
pane.setVisible(false);
FXTimer.runLater(Duration.ofMillis(1000), () -> pane.setVisible(false));
});
太棒了,谢谢!:)我选择了第二种解决方案,因为这需要更少的修改,而且效果很好:)
import java.time.Duration;
import org.reactfx.util.FxTimer;
button.setOnAction(event -> {
pane.setVisible(false);
FXTimer.runLater(Duration.ofMillis(1000), () -> pane.setVisible(false));
});