Javafx 2 在VBox中的按钮之间添加空格

Javafx 2 在VBox中的按钮之间添加空格,javafx-2,javafx,Javafx 2,Javafx,我有一系列按钮: VBox menuButtons = new VBox(); menuButtons.getChildren().addAll(addButton, editButton, exitButton); 我想在这些按钮之间添加一些间距,而不使用CSS样式表。我认为应该有办法做到这一点 setPadding()用于VBox中的按钮s setMargin()应该用于VBox本身。但是我没有找到一种方法来计算按钮之间的间距 我很高兴有任何想法。:) VBox支持间距: VBox men

我有一系列按钮:

VBox menuButtons = new VBox();
menuButtons.getChildren().addAll(addButton, editButton, exitButton);
我想在这些按钮之间添加一些间距,而不使用CSS样式表。我认为应该有办法做到这一点

setPadding()
用于
VBox中的
按钮
s

setMargin()
应该用于
VBox
本身。但是我没有找到一种方法来计算按钮之间的间距


我很高兴有任何想法。:)

VBox
支持间距:

VBox menuButtons = new VBox(5);


只需调用
setspace
方法并传递一些值。 带有
HBox
的示例(与
VBox
相同):

这就是它的样子:

没有间距限制:

间距:


如果您使用的是FXML,请使用
间距属性:

<VBox spacing="5" />

正如其他人提到的,您可以使用
setspace()

但是,您也可以使用
setMargin()
,它不是用于窗格(或框),而是用于单个
节点
setPadding()
方法用于窗格本身。事实上,
setMargin()
将节点作为参数,以便您可以猜测它的用途

例如:

HBox pane = new HBox();
Button buttonOK = new Button("OK");
Button buttonCancel = new Button("Cancel");
/************************************************/
pane.setMargin(buttonOK, new Insets(0, 10, 0, 0)); //This is where you should be looking at.
/************************************************/
pane.setPadding(new Insets(25));
pane.getChildren().addAll(buttonOK, buttonCancel);
Scene scene = new Scene(pane);
primaryStage.setTitle("Stage Title");
primaryStage.setScene(scene);
primaryStage.show();
如果将该行替换为

pane.setSpacing(10);

如果有多个节点应该隔开,
setspace()
方法要方便得多,因为您需要为每个节点调用
setMargin()
,这太荒谬了。但是,
setMargin()
如果需要节点周围的边距(duh),您可以确定每边的边距,因为
setspace()
方法只在节点之间放置空格,而不是在节点和窗口边缘之间。

这有帮助吗@安迪:不,我现在在用JavaFX,而不是Swing,但是谢谢!:)回答得好!大Thx!:)如果Sergey Grinev没有给出一个解决方案,我可以直接在构造函数中设置它,我会选择这个。在javafx 8中,构建器会贬值,您应该使用标准方法创建对象并设置其间距。看@FloC我相信你。但我使用的是Java1.7Update21。不过,感谢您指出。Thx…:$我认为这将是其中的元素数量。Never认为这很简单。顺便说一句:在FXML中,它看起来是这样的:
在这个简单问题上的票数确实让这个类的API得到了正确的评价:)@codepleb FXML版本就是我想要的。谢谢!好文章,但我认为做
HBox.setMargin(节点,插图)
比做
pane.setMargin(节点,插图)
要干净一些,因为
setMargin
是一个静态函数。我不知道这两种方式是否有实际的区别。
HBox pane = new HBox();
Button buttonOK = new Button("OK");
Button buttonCancel = new Button("Cancel");
/************************************************/
pane.setMargin(buttonOK, new Insets(0, 10, 0, 0)); //This is where you should be looking at.
/************************************************/
pane.setPadding(new Insets(25));
pane.getChildren().addAll(buttonOK, buttonCancel);
Scene scene = new Scene(pane);
primaryStage.setTitle("Stage Title");
primaryStage.setScene(scene);
primaryStage.show();
pane.setSpacing(10);