2016-11-26 97 views
0

由於某種原因,textExampleTwo.setLayoutX(40)實際上並不導致Text完全向右移動。這是一個錯誤還是我錯過了一些重要的東西?.setLayoutX()不影響FlowPane中的位置

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

你已經錯過了一些重要的位置:

許多Pane小號包括FlowPane決定對自己子女的位置。用於定位layoutXlayoutY屬性被使用。如果您爲其中的一個分配新值,並且Node是佈局中的一個孩子,它將自己定位到孩子身上,這隻會導致在下一個佈局過程中將位置改回。

對此的例外是Node s,managed屬性設置爲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的空間。如果在此節點之前添加足夠的子元素將其移動到第二列,則將使用此間距計算到列開頭的距離。

+0

啊,感謝您的幫助!還有一件事我想問一下,你也可以爲一個窗格做這個嗎? –

+0

取決於窗格的種類... – fabian

相關問題