gpt4 book ai didi

java - 如何在JavaFX中使TextFlow在窗口中央左对齐

转载 作者:行者123 更新时间:2023-11-30 05:19:59 25 4
gpt4 key购买 nike

如何让TextFlow中的文本左对齐,但TextFlow却在窗口的中央?

我尝试用VBox、StackPane和BorderPane来实现,但它们只能将文本居中对齐,或者使TextFlow位于窗口的左侧。

我需要的效果和IDEA类似: The effect i need

但是我实现的效果是这样的:

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception {
TextFlow text = new TextFlow(
new Text("Search Everywhere\n"),
new Text("Project View\n"),
new Text("Go to File\n")
);
text.setTextAlignment(TextAlignment.LEFT);
VBox root = new VBox(text);
root.setAlignment(Pos.CENTER);

primaryStage.setScene(new Scene(root, 400, 400));
primaryStage.show();
}
}

enter image description here

最佳答案

感谢c0der的提示,我找到了FlowPane可以轻松实现这样的效果:

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception {
TextFlow text = new TextFlow(
new Text("Search Everywhere\n"),
new Text("Project View\n"),
new Text("Go to File\n")
);
text.setTextAlignment(TextAlignment.LEFT);
FlowPane root = new FlowPane(text);
root.setAlignment(Pos.CENTER);

primaryStage.setScene(new Scene(root, 400, 400));
primaryStage.show();
}
}

effect

关于java - 如何在JavaFX中使TextFlow在窗口中央左对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59720341/

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