如果在布局

时间:2015-11-20 20:33:28

标签: javafx jython race-condition

如果我在将节点添加到Node.getBoundsInParent()后过早地调用GridPane ,则返回的Bounds对象的成员都是0.0 。看,一些Java代码演示了我的问题:

import java.lang.Runnable;
import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;

public class MyApplication extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        List<Label> labels = new ArrayList<Label>();
        for (int index = 0; index < 12; ++index) {
            labels.add(new Label(String.format("%d", index)));
        }

        GridPane gridPane = new GridPane();

        Button layoutButton = new Button("Layout");
        layoutButton.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {
                    gridPane.getChildren().setAll(labels);
                    for (int index = 0; index < labels.size(); ++index) {
                        gridPane.setConstraints(labels.get(index), 0, index);
                    }
                }
            });

        Button getBoundsButton = new Button("Get Bounds");
        getBoundsButton.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {
                    System.out.println(labels.get(0).getBoundsInParent());
                }
            });

        Button layoutAndGetBoundsButton = new Button("Layout and Get Bounds");
        layoutAndGetBoundsButton.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {
                    gridPane.getChildren().setAll(labels);
                    for (int index = 0; index < labels.size(); ++index) {
                        gridPane.setConstraints(labels.get(index), 0, index);
                    }

                    System.out.println(labels.get(0).getBoundsInParent());
                }
            });

        Button layoutAndGetBoundsLaterButton = new Button("Layout and Get Bounds Later");
        layoutAndGetBoundsLaterButton.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {
                    gridPane.getChildren().setAll(labels);
                    for (int index = 0; index < labels.size(); ++index) {
                        gridPane.setConstraints(labels.get(index), 0, index);
                    }

                    class MyRunnable implements Runnable {
                        Label label;
                        public MyRunnable(Label label) {
                            this.label = label;
                        }
                        @Override
                        public void run() {
                            System.out.println(this.label.getBoundsInParent());
                        }
                    }

                    Platform.runLater(new MyRunnable(labels.get(0)));
                }
            });

        VBox vbox = new VBox();
        vbox.getChildren().setAll(
            layoutButton,
            getBoundsButton,
            layoutAndGetBoundsButton,
            layoutAndGetBoundsLaterButton);

        HBox hbox = new HBox();
        hbox.getChildren().setAll(vbox, gridPane);

        primaryStage.setTitle("Race Condition?");
        primaryStage.setScene(new Scene(hbox, 320, 240));
        primaryStage.show();
    }

}

以下是我的问题中首次出现的Jython:

import java.lang.Runnable as Runnable
import javafx.application.Application as Application
import javafx.application.Platform as Platform
import javafx.event.EventHandler as EventHandler
import sys

class MyApplication(Application):

    @classmethod
    def main(cls, args):
        MyApplication.launch(cls, args)

    def start(self, primaryStage):
        import javafx.scene.Scene as Scene
        import javafx.scene.control.Button as Button
        import javafx.scene.control.Label as Label
        import javafx.scene.layout.GridPane as GridPane
        import javafx.scene.layout.HBox as HBox
        import javafx.scene.layout.VBox as VBox

        labels = [Label('%d' % index) for index in range(12)]
        gridPane = GridPane()

        layoutButton = Button('Layout')
        def _handler(event):
            gridPane.getChildren().setAll(labels)
            for index, label in enumerate(labels):
                gridPane.setConstraints(label, 0, index)
        layoutButton.setOnAction(_handler)

        getBoundsButton = Button('Get Bounds')
        def _handler(event):
            print labels[0].getBoundsInParent()
        getBoundsButton.setOnAction(_handler)

        layoutAndGetBoundsButton = Button('Layout and Get Bounds')
        def _handler(event):
            gridPane.getChildren().setAll(labels)
            for index, label in enumerate(labels):
                gridPane.setConstraints(label, 0, index)
            print labels[0].getBoundsInParent()
        layoutAndGetBoundsButton.setOnAction(_handler)

        layoutAndGetBoundsLaterButton = Button('Layout and Get Bounds Later')
        def _handler(event):
            gridPane.getChildren().setAll(labels)
            for index, label in enumerate(labels):
                gridPane.setConstraints(label, 0, index)
            class MyRunnable(Runnable):
                def __init__(self, label):
                    self.label = label
                def run(self):
                    print self.label.getBoundsInParent()
            Platform.runLater(MyRunnable(labels[0]))
        layoutAndGetBoundsLaterButton.setOnAction(_handler)

        vbox = VBox()
        vbox.getChildren().setAll([
                layoutButton,
                getBoundsButton,
                layoutAndGetBoundsButton,
                layoutAndGetBoundsLaterButton])

        hbox = HBox()
        hbox.getChildren().setAll([vbox, gridPane])

        primaryStage.setTitle('Race Condition?')
        primaryStage.setScene(Scene(hbox, 320, 240))
        primaryStage.show()

MyApplication.main(sys.argv)
  • 如果我点击&#39;布局&#39;和&#39;获得边界&#39;按顺序按钮,我的程序将按预期打印一个非零成员的边界框。

  • 如果我点击&#39;布局和获取边界&#39;按钮,我的程序将打印一个边框,所有成员都设置为零。 (单击此按钮第二时间将打印预期结果。)

  • 如果我点击&#39;布局并稍后获取边界&#39;按钮,结果没有更好。

顺便说一下,我现在正在看GridPane这个问题,但是我看到了其他JavaFX控件的类似问题:我要求它做一件事,然后让它做另一件事事情,但还没有准备好做第二件事。

我做错了什么?

更新:我似乎与this issue发生冲突,因为这不是问题而已关闭。

1 个答案:

答案 0 :(得分:0)

我有一个解决方法,但它很难看,而且不太完全可靠。

我的解决方法是将对Node.getBoundsInParent()的调用放在事件处理程序中,然后使用动画将其延迟一毫秒:

def printBounds(event):
    print labels[0].getBoundsInParent()

import javafx.animation.Timeline as Timeline
import javafx.animation.KeyFrame as KeyFrame
import javafx.util.Duration as Duration

timeline = Timeline([
        KeyFrame(
            Duration.millis(1), printBounds, [])])
timeline.play()

这足以使GridPane稳定下来,以便Node返回正确的界限。

请告诉我有更好的方法。