我当前有一个GridPane作为布局的根,并且我有另一个包含2个标签的GridPane。我正在尝试让其中一个标签左对齐,另一个右对齐。
我试过使用GridPane.halignment="RIGHT",但这对标签没有任何影响。
这是我目前使用的布局:

这是我想要的布局:

我的布局代码如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.canvas.Canvas?>
<?import javafx.scene.control.Label?>
<GridPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="com.bbf.layout.MainLayout">
<GridPane fx:id="stats" GridPane.columnIndex="0" GridPane.rowIndex="0" GridPane.halignment="RIGHT" style="-fx-background-color: red;">
<Label fx:id="frame_count_label" text="Frame: 123" GridPane.columnIndex="0" GridPane.rowIndex="0" style="-fx-background-color: blue;"/>
<Label fx:id="processing_time_label" text="Processed in 45 ms." GridPane.columnIndex="1" GridPane.rowIndex="0" GridPane.halignment="RIGHT" style="-fx-background-color: green;"/>
</GridPane>
<Canvas fx:id="video_canvas" GridPane.columnIndex="0" GridPane.rowIndex="1" width="640" height="480"/>
<GridPane fx:id="kb_shortcuts" GridPane.columnIndex="0" GridPane.rowIndex="2">
<Label text="[E] Toggle Overlay" GridPane.columnIndex="0" GridPane.rowIndex="0"/>
<Label text="[S] Previous Frame" GridPane.columnIndex="1" GridPane.rowIndex="0" />
<Label text="[D] Next Frame" GridPane.columnIndex="2" GridPane.rowIndex="0" />
</GridPane>
</GridPane>如何才能按我想要的方式对齐这些标签?对于我正在尝试做的事情,GridPane是不是很理想?我对JavaFX相当缺乏经验,但对Android的布局管理器很熟悉。
发布于 2019-05-25 16:45:19
嵌套的GridPane中的第二列不够大,无法使对齐产生任何影响。只需将GridPane.hgrow="ALWAYS"添加到标签中,即可将其移动到我想要的位置。
https://stackoverflow.com/questions/56301362
复制相似问题