我想出了如何使用 aHBox
而不是 aToolBar
来控制控件;关键是HBox.setHgrow()
方法,它允许您将间隔对象设置为增长以填充可用空间。我仍然不知道是否可以使用实际的 ToolBar 实例来做到这一点。
/**
* Creates and populates the Node that serves as the window toolbar.
*
* @return a newly constructed and populated toolbar component
*/
private Node makeToolbar() {
// Auto-sizing spacer
Region spacer = new Region();
HBox.setHgrow(spacer, Priority.ALWAYS);
// Horizontal box containing toolbar controls
HBox box = new HBox();
box.setPadding(new Insets(8));
box.setAlignment(Pos.CENTER);
box.getChildren().addAll(openButton, spacer, resizeSlider);
// Colored background panel with drop shadow
Pane bgRect = new Pane();
bgRect.setStyle("-fx-background-color: #e0e0e0;");
bgRect.setEffect(DropShadowBuilder.create().width(1).build());
// StackPane to hold box and rectangle
StackPane stack = new StackPane();
stack.getChildren().addAll(bgRect, box);
return stack;
}