1

鉴于此代码:

@Override
public void start(final Stage stage)
{
    final StackPane root = new StackPane();
    final Region r1 =  new Region(), r2 = new Region(), r3 = new Region();

    r1.setMaxSize(400, 400);
    r1.setStyle("-fx-background-color: yellow");
    StackPane.setAlignment(r1, Pos.TOP_LEFT);

    r2.setMaxSize(400, 400);
    r2.setStyle("-fx-background-color: black");
    StackPane.setAlignment(r2, Pos.TOP_RIGHT);

    r3.setMaxSize(400, 400);
    r3.setStyle("-fx-background-color: blue");
    StackPane.setAlignment(r3, Pos.BOTTOM_CENTER);

    root.getChildren().addAll(r1, r2 ,r3);

    final Scene scene = new Scene(root, 600, 600);
    stage.setScene(scene);
    stage.show();
}

输出如下:

在此处输入图像描述

是否有任何简单的方法来控制元素优先级,而无需删除并重新添加以正确顺序跟随的所有元素?在这种特殊情况下,我想要做的是使具有yellow背景的元素覆盖其他两个,从而导致:

在此处输入图像描述

可以用Scene Builder移动元素CTRL+([|])。不过,我目前能想到的唯一方法是,在代码中执行此操作时,获取根元素的子元素并以正确的顺序执行一对删除/添加。有没有其他方法,比如管理覆盖优先级的一些属性?谢谢你。

4

0 回答 0