GridPane中Label的JavaFX对齐 [英] JavaFX alignment of Label in GridPane

查看:1860
本文介绍了GridPane中Label的JavaFX对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑为什么设置Label.setAlignment(Pos.CENTER_RIGHT)不会影响随后添加到GridPane中的标签的对齐方式?唯一的方法是通过网格(例如ColumnConstraints)或者通过例如将标签添加到具有右对齐的HBox中。



为什么将标签的对齐设置为CENTER_RIGHT不起作用?我可以看到API说:指定Labeled内的文本和图形如何在Labeled内部有空的时候对齐。但是,如何获得标签中的空白空间?

label.setAlignment(Pos.CENTER_RIGHT); 使用 GridPane.setHalignment(label,HPos.RIGHT); p>

JavaFX使用自顶向下布局。因此,场景将根节点调整到场景的大小,根节点的大小和每个子节点的位置取决于其布局策略和场景赋予的空间量,然后每个子节点调整其大小并定位其子节点这取决于它自己的布局策略和它可用的空间量等等。下面的场景图表就是这样。



根据 标签 alignmentProperty


指定文本和图形当标签内有空白空间时,标签应该对齐。

当然,标签的可用空间量取决于其父,在这种情况下是网格窗格。您当然可以了解网格窗格的布局策略以及如何通过阅读它的文档。简而言之,虽然:

默认情况下,网格窗格将为每个节点分配其首选大小,并且,如果它放置的单元格具有更多空间,则会将节点在网格单元的左上角。标签的首选尺寸当然是计算的尺寸:只是足够大以容纳其内容。因此,您会发现只需将标签放置在网格窗格中并在标签上设置对齐方式将不会产生任何效果,因为标签的大小足以容纳其内容,并且没有多余的空间来对齐文本/图形。您可以通过在标签上放置背景颜色或边框来形象化。



因此,您可以将标签上的对齐设置为 CENTER_RIGHT ,然后指示网格窗格让标签增长。这需要两件事:首先,告诉网格窗格让标签填充单元格的宽度:

  GridPane.setFillWidth标签,真); 

,并且,由于网格窗格也将遵守子节点的最大大小,并且标签默认使用它的首选尺寸作为其最大尺寸允许标签增长:

  label.setMaxWidth(Double.MAX_VALUE); 

然后标签会增长到单元格的大小,所以如果您还有

  label.setAlignment(Pos.CENTER_RIGHT); 

它会将自己的内容调整到正确的位置。



更明智的方法可能只是告诉网格窗格如何对齐单元格中的标签:

  GridPane .setHalignment(label,HPos.RIGHT); 

然后让标签呈现其默认大小和对齐方式,所有内容都可以正常使用。



您还可以使用 ColumnConstraints 对象为特定列中的所有标签设置默认对齐方式,这样更方便如果你正在建立一个表格。


I'm confused as to why setting the Label.setAlignment(Pos.CENTER_RIGHT) does not affect the alignment of a label that is then added into a GridPane? The only way to do it is seemingly through the grid (e.g. ColumnConstraints) or by e.g. adding the Label to a HBox that has right alignment.

Why does setting the alignment of the label to CENTER_RIGHT have no effect? I can see the API says: "Specifies how the text and graphic within the Labeled should be aligned when there is empty space within the Labeled." But how do I get empty space in a label?

解决方案

TL:DR version: instead of label.setAlignment(Pos.CENTER_RIGHT); use GridPane.setHalignment(label, HPos.RIGHT);.

JavaFX uses a top-down layout. So the scene sizes the root node to the size of the scene, the root node sizes and positions each of its child nodes depending on its layout strategy and the amount of space the scene gave it, each child node then sizes and positions its child nodes depending on its own layout strategy and the amount of space it has available, and so on down the scene graph.

According to the documentation for Label, the alignmentProperty

Specifies how the text and graphic within the Labeled should be aligned when there is empty space within the Labeled.

Of course, the amount of space available to the label is determined by its parent, which in this case is the grid pane. You can of course find out about the grid pane's layout strategy and how to configure it by reading its documentation. In brief, though:

By default, a grid pane will allocate each node its preferred size, and, if the cell it's placed in has additional space, will align the node in the top left of the grid cell. The preferred size for a label is of course the computed size: just big enough to hold its content. Hence you see that simply placing a label into a grid pane and setting alignment on the label will not have any effect, because the label is sized just large enough to hold its content, and there is no extra space to align the text/graphic. You can visualize this by placing a background color or border on the label.

So you could set the alignment on the label to CENTER_RIGHT, and then instruct the grid pane to let the label grow. This needs two things: first, tell the grid pane to let the label fill the width of the cell:

GridPane.setFillWidth(label, true);

and, since the grid pane will also respect the child node's maximum size, and the label by default uses its preferred size as its maximum size, allow the label to grow:

label.setMaxWidth(Double.MAX_VALUE);

Then the label will grow to the size of the cell, and so if you also have

label.setAlignment(Pos.CENTER_RIGHT);

it will align its own content to the right.

A more sensible approach is probably just to tell the grid pane how to align the label in the cell:

GridPane.setHalignment(label, HPos.RIGHT);

Then let the label take on its default size and alignment and everything will work.

You can also use a ColumnConstraints object to set the default alignment for all labels in a particular column, which is by far the more convenient approach if you are building a form.

这篇关于GridPane中Label的JavaFX对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆