问题
I have a simple BorderPane
instance which is the root node of my Scene
. The border pane include a panel on the right side with some GUI controls, including a button. When I click on this button, I create an instance of class (Foo
) which extends StackPane
and I put this instance as a center node of the border pane.
I would like to get the width and height values of the available center space of the border pane from the Foo
instance by calling this.getLayoutBounds.getWidth()
, respectively ...getHeight()
but it always returns zero?
回答1:
Add a listener to the StackPane's layoutBoundsProperty or bind to it's height and width properties.
JavaFX will just return zero for the height and width of the StackPane until it has executed a layout pass on it. Because a StackPane is a resizable node, JavaFX usually does not know what size it will be until it has been added to a scene and shown on a Stage.
If you only want to know it's initial size, you can call getHeight or getWidth on the StackPane after you have invoked stage.Show.
Future layout passes may resize the StackPane as it's available area changes, hence the recommendation to use listeners or binds if you want to always be informed of the current pane size.
来源:https://stackoverflow.com/questions/12643125/getting-the-width-and-height-of-the-center-space-in-a-borderpane-javafx-2