gpt4 book ai didi

java - 如何对齐对象并设置彼此之间的间距?使用 JavaFX 和 Java 进行编程

转载 作者:行者123 更新时间:2023-12-02 00:26:53 24 4
gpt4 key购买 nike

我一直在使用 JavaFX,我真的很喜欢它。但我似乎无法将我的内容放在我想要的地方。即使我在中心放置一个 vbox,在顶部放置一个标签,它们仍然位于中心并且几乎互相接触。

我想要(当然在屏幕中央):

My Title


SubLabel
Button1
Button2
Button3


BottomLabel

但它显示为:

My Title
SubLabel
Button1
Button2
Button3
BottomLabel

如果我 settranslateX 我的 vbox 或顶部标签,它会移动标签,但也会随之移动其他所有内容。

怎样才能让它正确对齐?

这是我的伪代码:

private Node PreMenu()
{

Group group2 = new Group();

BorderPane pane = new BorderPane();

Text label = new Text("Please Choose a Option");
label.setFont(Font.font("Kozuka Gothic Pro", 30));
label.setEffect(addEffect(Color.web("#FF6600"), .85, 20));
label.setTextAlignment(TextAlignment.CENTER);
label.setTranslateY(-300); //This moves my label, but it also moves the vbox under it DOWN.

VBox vbox = new VBox();

Button option1= new Button("Firstbutton");
Button option2= new Button("Secondbutton");
Button option3= new Button("HelpButton");
option1.setEffect(addEffect(Color.web("#FF6600"), .8, 10));
option2.setEffect(addEffect(Color.web("#FF6600"), .8, 10));
option3.setEffect(addEffect(Color.web("#FF6600"), .8, 10));
option1.setTextAlignment(TextAlignment.CENTER);
option1.setMinWidth(400);
option2.setTextAlignment(TextAlignment.CENTER);
option2.setMinWidth(400);
option3.setTextAlignment(TextAlignment.CENTER);
option3.setMinWidth(400);

vbox.setSpacing(20);
vbox.getChildren().add(option1);
vbox.getChildren().add(option2);
vbox.getChildren().add(option3);

pane.setTop(label);
pane.setCenter(vbox);

group2.getChildren().add(pane);

return group2;
}

我的程序中的另一个节点也有同样的问题:

最佳答案

使用代码的最简单方法是使用按钮为 Vbox 设置顶部和底部填充:

vbox.setPadding(new Insets(50,0,50,0));

此行在 vbox 上方和下方添加 50 像素的间距。

关于java - 如何对齐对象并设置彼此之间的间距?使用 JavaFX 和 Java 进行编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9816212/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com