.setLayoutX()不影响FlowPane中的位置

时间:2016-11-26 15:31:26

标签: javafx javafx-8

由于某种原因textExampleTwo.setLayoutX(40)实际上并未导致Text向右移动。这是一个错误还是我错过了一些重要的东西?

public void start(Stage stage) throws Exception {

    FlowPane flowPane = new FlowPane();
    flowPane.setOrientation(Orientation.VERTICAL);

    Text textExampleOne = new Text("An example - 1");

    Text textExampleTwo = new Text("An example - 2");
    textExampleTwo.setLayoutX(40.0);

    flowPane.getChildren().addAll(textExampleOne, textExampleTwo);

    Scene applicationScene = new Scene(flowPane);
    stage.setHeight(400.0);
    stage.setWidth(400.0);
    stage.setScene(applicationScene);
    stage.show();
}

An image of the application (Note that they are on the same X level)

1 个答案:

答案 0 :(得分:1)

你错过了一些重要的事情:

包括Pane在内的许多FlowPane确定了孩子自己的位置。对于定位,使用layoutXlayoutY属性。如果您为其中一个分配了一个新值,并且Node是一个布局的子项,它会定位它的子项本身,这只会导致在下一个布局过程中更改回位置。

例外情况是Nodemanaged属性设置为false。这会导致 layoutX layoutY被分配。

在你的情况下,你似乎想要两者的结合。

在这种情况下,可以通过设置边距来实现所需的效果。

// set all insets except the left one to 0
FlowPane.setMargin(textExampleOne, new Insets(0, 0, 0, 40));

但请注意,这不会将x位置设置为40,但会在Node的左侧保留40的空格。如果在此节点之前添加足够的子节点以将其移动到第二列,则此间距将用于计算到列开头的距离。