GridPane 中标签的 JavaFX 对齐方式 [英] JavaFX alignment of Label in GridPane

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

问题描述

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

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.

为什么将标签的对齐方式设置为 CENTER_RIGHT 没有效果?我可以看到 API 说:当 Labeled 中有空白空间时,指定 Labeled 中的文本和图形应该如何对齐."但是如何在标签中获得空白空间?

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:代替 label.setAlignment(Pos.CENTER_RIGHT); 使用 >GridPane.setHalignment(label, HPos.RIGHT);.

JavaFX 使用自上而下的布局.所以场景将根节点的大小设置为场景的大小,根节点根据其布局策略和场景给它的空间量来调整每个子节点的大小和位置,然后每个子节点调整其子节点的大小和位置取决于它自己的布局策略和它可用的空间量,等等.

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.

根据 LabelalignmentProperty

指定当 Labeled 内有空白空间时,Labeled 内的文本和图形应如何对齐.

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.

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

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.

您还可以使用 ColumnConstraints 对象为特定列中的所有标签设置默认对齐方式,如果您正在构建表单,这是迄今为止更方便的方法.

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 中标签的 JavaFX 对齐方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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