JavaFX在一段时间后隐藏节点

时间:2016-08-25 14:52:40

标签: java javafx

存在Stage,其中包含一些简单节点,例如Circle布局中的Rectangle(右)和BordePane(左)。

  1. 我想要的是如果鼠标没有被移动1秒钟(在窗口中),圆圈就会消失。

  2. 如果elememt在最后1秒内未输入Rectangle,则该矩形将消失。

  3. 我正在考虑使用Thread,但它看起来并不正确。

    是否有JavaFX个包来执行此操作?有些代码会感激不尽。

2 个答案:

答案 0 :(得分:3)

使用几个PauseTransition s:

PauseTransition hideCircle = new PauseTransition(Duration.seconds(1));
PauseTransition hideRectangle = new PauseTransition(Duration.seconds(1));

// restart hideCircle if mouse moves in window:
scene.addEventFilter(MouseEvent.MOUSE_MOVED, e -> hideCircle.playFromStart());

// restart hideRectangle if mouse enters rectangle:
rectangle.setOnMouseEntered(e -> hideRectangle.playFromStart());

// hide circle if pause gets to end:
hideCircle.setOnFinished(e -> circle.setVisible(false));

// hide rectangle if pause gets to end:
hideRectangle.setOnFinished(e -> rectangle.setVisible(false));

// start "timers":
hideCircle.play();
hideRectangle.play();

您可能更愿意从父级中移除形状,而不是将visible设置为false,具体取决于您要执行的操作。

答案 1 :(得分:0)

使用javafx.concurrent.Task<V>(doc:https://docs.oracle.com/javafx/2/api/javafx/concurrent/Task.html)实现一个可以对您的更改做出反应的后台侦听器,一旦您想对GUI元素执行某些操作,请在Platform.runLater()

相关问题