简体   繁体   English

Javafx GridPane,如何居中居中? (文本/标签)

[英]Javafx GridPane, how to center Node? (Text/Label)

I am trying to learn Javafx for a class I am taking. 我正在尝试上一门课程学习Javafx。 I am having a lot of difficulty transitioning from HTML + ERB templates to the Javafx framework (I am a Rails dev). 从HTML + ERB模板过渡到Javafx框架时,我遇到了很多困难(我是Rails开发人员)。

For some reason I am unable to center a Label node in a gridpane. 由于某种原因,我无法将Label节点居中于网格窗格中。 Here is my start method: 这是我的start方法:

@Override
    public void start(Stage stage) throws Exception {
        GridPane root = new GridPane();
        FlowPane leftbanner = new FlowPane();

        leftbanner.setPrefWidth(200);
        String bgStyle = "-fx-background-color: lightblue;"
                         + "-fx-background-radius: 0%;"
                         + "-fx-background-inset: 5px;";
        leftbanner.setStyle(bgStyle);

        root.add(leftbanner, 0, 0, 1, 1);
        root.add(createGridPane(), 1, 0, 1, 1);

        Scene scene = new Scene(root, 700, 500);
        stage.setTitle("Recommendify");
        stage.setScene(scene);
        stage.show();
    }

I am using the createGridPane method to build my app. 我正在使用createGridPane方法来构建我的应用程序。 Here are the contents of that method which includes my attempts to center the label: 这是该方法的内容,其中包括我尝试使标签居中的尝试:

public GridPane createGridPane() {
        GridPane grid = new GridPane();
        grid.setPadding(new Insets(10));
        grid.setHgap(10);
        grid.setVgap(10);

        Text txt = new Text("Recommendify");
        txt.setFont(Font.font("Dialog", FontWeight.BOLD, 12));
        GridPane.setHalignment(txt, HPos.CENTER);

        grid.add(txt, 0, 0, 1, 1);
        grid.add(new Separator(), 0, 1, 3, 1);

        return grid;
}

I have also tried these solutions posted here: JavaFX alignment of Label in GridPane 我也尝试过以下发布的解决方案: GridPane中Label的JavaFX对齐

Do I need to throw this Label node into a container in order to center it in the GridPane? 我是否需要将此Label节点放入容器中以使其在GridPane中居中? Like an HBox or VBox? 像HBox还是VBox?

There is actually nothing wrong with your code. 您的代码实际上没有错。 The "Recommendify" text is centered in its cell. 该“Recommendify”文本其细胞中心。 However, the cell is no wider than the text itself. 但是,单元格的宽度不超过文本本身。

You can see this by turning on the grid lines within your createGridPane() method: 您可以通过在createGridPane()方法中打开网格线来查看此信息:

grid.setGridLinesVisible(true);

截图


To test the alignment, you can add ColumnConstraints to the GridPane : 要测试对齐方式,可以将ColumnConstraints添加到GridPane

grid.getColumnConstraints().add(new ColumnConstraints(150));

The above statement sets the preferred width of the first column to 150 . 上面的语句将第一列的首选宽度设置为150 Here is the new result: 这是新结果:

屏幕截图2

As you can see, the Text node is centered properly. 如您所见,“ Text节点正确居中。

EDIT: Keep in mind that if you want your Text to be centered over the Separator you've added to the second row, you need to have it span 3 columns as well. 编辑: 请记住,如果要使Text在添加到第二行的Separator上居中,则还需要使其跨3列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM