我有一组按钮:
VBox menuButtons = new VBox();
menuButtons.getChildren().addAll(addButton, editButton, exitButton);
Run Code Online (Sandbox Code Playgroud)
我想在这些按钮之间添加一些间距,而不使用CSS表.我认为应该有办法解决这个问题.
setPadding(); 是为了盒子里的按钮.setMargin(); 应该是Box本身.但我没有找到按钮间距的方法.
任何想法我都很高兴.:)
Ser*_*nev 55
VBox 支持间距:
VBox menuButtons = new VBox(5);
Run Code Online (Sandbox Code Playgroud)
要么
menuButtons.setSpacing(5);
Run Code Online (Sandbox Code Playgroud)
Bra*_*zic 17
只需调用setSpacing方法并传递一些值.示例HBox(对于它是相同的VBox):
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.HBoxBuilder;
import javafx.stage.Stage;
public class SpacingDemo extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
stage.setTitle("Spacing demo");
Button btnSave = new Button("Save");
Button btnDelete = new Button("Delete");
HBox hBox = HBoxBuilder.create()
.spacing(30.0) //In case you are using HBoxBuilder
.padding(new Insets(5, 5, 5, 5))
.children(btnSave, btnDelete)
.build();
hBox.setSpacing(30.0); //In your case
stage.setScene(new Scene(hBox, 320, 240));
stage.show();
}
}
Run Code Online (Sandbox Code Playgroud)
这就是它的样子:
没有间距:

间距:

正如其他人提到的,你可以使用setSpacing().
但是,您也可以使用setMargin(),它不适用于窗格(或您的文字中的方框),它适用于个人Node.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();
Run Code Online (Sandbox Code Playgroud)
如果用这条线代替,你可以获得相同的结果
pane.setSpacing(10);
Run Code Online (Sandbox Code Playgroud)
如果你有几个应该间隔的节点,setSpacing()方法更方便,因为你需要调用setMargin()每个单独的节点,这将是荒谬的.但是,setMargin()如果您需要在节点周围使用边距(duh),您可以确定每边的边距,因为setSpacing()方法只在节点之间放置空格,而不是在节点和窗口边缘之间放置空格.