简体   繁体   English

GridPane(JavaFX)中的居中按钮和文本

[英]Centering buttons and text in a GridPane (JavaFX)

I have a simple display that has a title at the very top, a list in the middle, and then three buttons at the bottom. 我有一个简单的显示,顶部有一个标题,中间有一个列表,底部有三个按钮。 I want the title at the top and the buttons at the bottom to be horizontally centered. 我希望顶部的标题和底部的按钮水平居中。

Here is the code that I've been trying to use: 这是我一直在尝试使用的代码:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.GridPane?>

<GridPane
    xmlns="http://javafx.com/javafx/8.0.40"
    xmlns:fx="http://javafx.com/fxml/1"
    fx:controller="controllers.SongLibraryController"
>
    <Label
        text="Todo List"
        GridPane.columnSpan="3"
        GridPane.rowIndex="0"
        GridPane.halignment="CENTER"
    />
    <ListView
        prefWidth="300"
        prefHeight="400"
        fx:id="listView"
        GridPane.columnSpan="3"
        GridPane.rowIndex="1"
    />
    <Button
        fx:id="editItem"
        GridPane.halignment="CENTER"
        GridPane.columnIndex="0"
        GridPane.rowIndex="2"
        text="Edit Item"
    />
    <Button
        fx:id="addItem"
        GridPane.halignment="CENTER"
        GridPane.columnIndex="1"
        GridPane.rowIndex="2"
        text="Add Item"
    />
    <Button
        fx:id="deleteItem"
        GridPane.halignment="CENTER"
        GridPane.columnIndex="2"
        GridPane.rowIndex="2"
        text="Delete Item"
    />
</GridPane>

And here is the current output: 这是当前输出:

在此处输入图片说明

I've tried doing various things like using alignment="CENTER" for my GridPane, but nothing seems to be working. 我尝试做各种事情,例如为GridPane使用alignment="CENTER" ,但似乎没有任何效果。 Any help would be appreciated! 任何帮助,将不胜感激!

You need to set alignment for your GridPane as alignment="center" and all elements will be aligned in center. 您需要将GridPane的alignment="center"设置为alignment="center" ,所有元素都将在中心对齐。

Update You can align your buttons in center like this. 更新您可以像这样在中心对齐按钮。

<HBox GridPane.rowIndex="2" GridPane.columnSpan="3" alignment="CENTER" spacing="25">
    <Button text="b1"/>
    <Button text="b2"/>
    <Button text="b3"/>
</HBox>

After this update you also can delete columnSpan attribute from each node. 更新之后,您还可以从每个节点中删除columnSpan属性。

To align elements inside a grid pane programatically, you will have to call static methods from the GridPane class. 若要以编程方式在网格窗格内对齐元素,则必须从GridPane类调用静态方法。

To set horizontal alignment, call GridPane.setHalignment(Node n, HPos p) . 要设置水平对齐方式,请调用GridPane.setHalignment(Node n, HPos p) To set vertical alignment, call GridPane.setValignment(Node n, VPos p) . 要设置垂直对齐方式,请调用GridPane.setValignment(Node n, VPos p)

You can read Oracle's own layout guide here . 您可以在此处阅读Oracle自己的布局指南。 If you scroll down until you see the pie chart, they show an example of how to manage layouts in grid panes. 如果向下滚动直到看到饼图,它们将显示如何在网格窗格中管理布局的示例。

To manage it via FXML, you will have to set a certain property. 要通过FXML对其进行管理,您将必须设置某个属性。 I can't remember of the top of my head, but I think it is alignment="center" in your GridPane node. 我不记得我的头顶,但我认为它是GridPane节点中的alignment="center"

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

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