我似乎找不到一种方法来注册场景或节点类的事件侦听器,该事件侦听器将在显示该场景时被调度。
我希望我的场景类是自包含的,这样我就可以使用构建器类来构造它们,并在它们显示时触发它们的任何动画。例如,我希望能够在我的Application类中执行以下操作...
public void start(Stage primaryStage) {
primaryStage.setScene(AnimatedLoginSceneBuilder.create()
.width(1024)
.height(768)
.frameRate(25)
.build();
)
primaryStage.show();
}我的AnimatedLoginSceneBuilder类创建一个场景和一个动画,并将其绑定到场景中的一些节点。但是,我只能返回带有build方法的场景(不能返回动画类)。例如,它看起来像这样……
public class AnimatedLoginSceneBuilder implements Builder<Scene> {
// private members such as width, height and framerate
// methods to set width, height and framerate (e.g. width(double width))
public Scene build() {
DoubleProperty x = new SimpleDoubleProperty();
Text node = TextNodeBuilder...
node.xProperty().bind(x);
final Timeline animation = TimelineBuilder... // animate x
return SceneBuilder.create()
. // create my scene using builders (bar the node above)
.build();
}
}但是我没有办法播放动画,所以我想有一些钩子像…
public class AnimatedLoginSceneBuilder ... {
...
public Scene build() {
...
final Timeline animation = TimelineBuilder... // animate x
return SceneBuilder.create()
. // create scene declaratively
.onShow(new EventHandler<SomeEvent>() {
@Overide public void handleSomeEvent() {
animation.play();
}
.build()
}然后,当场景显示时,它将自动播放。要求太多了吗?
一种替代方法是让构建器类同时返回包装在对象中的场景和动画,并执行类似于...
public void start(Stage primaryStage) {
WrapperObj loginSceneWrapper = AnimatedLoginSceneBuilder.create()
.width(1024)
.height(768)
.frameRate(25)
.build();
primaryStage.setScene(wrapperObj.getScene());
primaryStage.show();
wrapperObj.getAnimation().play();但这不是我想要的,因为我希望能够从现有的场景中切换到新的场景,而不做任何假设。例如,我希望在场景中有一个事件处理程序,能够让舞台过渡到新的场景,因此,我只希望能够调用primaryStage.setScene(我想要转到的新场景)。
有什么想法吗?
我见过的最接近的是How to listen for WindowEvent.WINDOW_SHOWN in the nodes of the scene graph?,但它不适用于这种情况。
发布于 2012-12-01 00:15:26
当显示javafx.stage.Window时,会触发"Showing“事件。您可以使用window.setOnShowing()和window.setOnShown()为适当的事件设置侦听器。Scene是场景图的容器,没有关于显示/隐藏的逻辑。
我建议存储场景的动画根节点(从父类或其子类扩展),而不是场景。并将侦听器添加到根changed事件中,如下所示
stage.getScene().rootProperty().addListener(new ChangeListener<MyAnimatedParent>() {
@Override
public void changed(ObservableValue<? extends MyAnimatedParent> observable, MyAnimatedParent oldValue, MyAnimatedParent newValue) {
newValue.animate();
}
});https://stackoverflow.com/questions/13644644
复制相似问题