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

标签 java user-interface javafx

如何让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/

相关文章:

java - 由字符串数组填充的 JComboBox(使用 for 循环)未出现

java - 在一个循环中批量插入到不同的表

java - 如何在 Eclipse 中获取选定的代码?

c# - Android和C#之间的加密

asp.net-mvc-3 - 如何为下拉列表使用数据注释?

java - JavaFX 中的 ChangeListener : how can I make these generics parameters less ugly?

java - 更新 NetBeans 中的 JAR 库

c++ - GUI 开发 - 语言和构建器

java - 用各自的字段 Swing 排列标签

Javafx 按钮保持 "enabled"