JavaFX StackPane 边界未更新
JavaFX StackPane bounds not updated
当我运行下面的程序
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Ellipse;
import javafx.stage.Stage;
public class Test1 extends Application {
public static void main(String[] args) throws Exception {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
Pane topPane = new Pane();
Scene scene = new Scene(topPane, 600, 400);
StackPane sp = new StackPane();
Label l1 = new Label("1 2");
Ellipse e1 = new Ellipse(100, 50);
e1.setOpacity(0.5);
sp.getChildren().addAll(l1, e1);
e1.radiusXProperty().bind(l1.widthProperty());
e1.radiusYProperty().bind(l1.heightProperty());
topPane.getChildren().add(sp);
sp.relocate(200, 100);
sp.setStyle("-fx-border-color: RED;");
Platform.runLater(() -> {
//l1.setText("123");
//l1.setText("1 2");
});
stage.setScene(scene);
stage.show();
}
}
我只在文本标签周围得到一个红色框,但是当我取消注释上面 Platform.runLater() 块内的两行时,我在外部椭圆周围得到一个红色框,这就是我想要的.
所以在我看来,堆栈窗格的布局边界没有根据模型描述正确设置,因为边界仅由标签控件确定。但是当我在 Platform.runLater() 中强制失效时,布局边界就在它们应该在的位置。
为什么会发生这种情况,我该如何预防?我希望能够只指定我的 model/graph 然后它应该在第一场演出中正确呈现,或者?
在 stage.Show();
之后添加这个 sp.requestLayout();
当我运行下面的程序
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Ellipse;
import javafx.stage.Stage;
public class Test1 extends Application {
public static void main(String[] args) throws Exception {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
Pane topPane = new Pane();
Scene scene = new Scene(topPane, 600, 400);
StackPane sp = new StackPane();
Label l1 = new Label("1 2");
Ellipse e1 = new Ellipse(100, 50);
e1.setOpacity(0.5);
sp.getChildren().addAll(l1, e1);
e1.radiusXProperty().bind(l1.widthProperty());
e1.radiusYProperty().bind(l1.heightProperty());
topPane.getChildren().add(sp);
sp.relocate(200, 100);
sp.setStyle("-fx-border-color: RED;");
Platform.runLater(() -> {
//l1.setText("123");
//l1.setText("1 2");
});
stage.setScene(scene);
stage.show();
}
}
我只在文本标签周围得到一个红色框,但是当我取消注释上面 Platform.runLater() 块内的两行时,我在外部椭圆周围得到一个红色框,这就是我想要的.
所以在我看来,堆栈窗格的布局边界没有根据模型描述正确设置,因为边界仅由标签控件确定。但是当我在 Platform.runLater() 中强制失效时,布局边界就在它们应该在的位置。
为什么会发生这种情况,我该如何预防?我希望能够只指定我的 model/graph 然后它应该在第一场演出中正确呈现,或者?
在 stage.Show();
sp.requestLayout();