【发布时间】:2014-04-10 20:14:15
【问题描述】:
有什么方法可以等待场景重绘吗?
我的问题是,我想使用 getChildren().add() 在窗格中添加注释,然后使用 Node.fireEvent(event) 在此节点上触发事件。
但是事件没有执行。我认为问题在于,在发生火灾时没有重新绘制场景,因此此时节点不是新场景的一部分。
所以最好的方法是等待场景重新绘制,然后触发我认为的事件。
【问题讨论】:
有什么方法可以等待场景重绘吗?
我的问题是,我想使用 getChildren().add() 在窗格中添加注释,然后使用 Node.fireEvent(event) 在此节点上触发事件。
但是事件没有执行。我认为问题在于,在发生火灾时没有重新绘制场景,因此此时节点不是新场景的一部分。
所以最好的方法是等待场景重新绘制,然后触发我认为的事件。
【问题讨论】:
我不知道你在这里使用的是哪个 UI 组件,但是,试着找出你的组件的 invalidate() 方法(或类似的东西),让它最终更新屏幕。
【讨论】:
你能发布代码吗?这对我来说很好:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class NewNodeEventTest extends Application {
@Override
public void start(Stage primaryStage) {
HBox root = new HBox(5);
Button newNodeButton = new Button("Add button");
newNodeButton.setOnAction(event -> {
Button newButton = new Button("Button");
newButton.setOnAction(e -> System.out.println("New button pressed"));
root.getChildren().add(newButton);
ActionEvent evt = new ActionEvent(newButton, newButton);
newButton.fireEvent(evt);
});
root.getChildren().add(newNodeButton);
Scene scene = new Scene(root, 250, 100);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
【讨论】: