简体   繁体   English

标签对齐方式很奇怪

[英]Labels alignment behaves weird

I have a panel that contains two labels. 我有一个包含两个标签的面板。 Such panels are used in a statistics block as "name:value". 此类面板在统计信息块中用作“名称:值”。 But name's length is different for every entry and UI looks ugly at the end. 但是每个条目的名称长度都不同,并且UI的结尾看起来很丑。 I decided to get the size of the biggest label and set setPrefWidth() for all labels in the column to that size. 我决定获取最大标签的大小,并将列中所有标签的setPrefWidth()设置setPrefWidth()大小。 But UI starts looking weird at the end. 但是UI最终开始看起来很奇怪。

Here is what I have: 这是我所拥有的:

在此处输入图片说明

Here is the code of such panel: 这是该面板的代码:

public class StatLine extends HBox {
    private Label name;
    private Label value;

    public StatLine(String name) {
        super();
        this.name = new Label(name);
        this.value = new Label("99999");

        this.name.setPrefWidth(25);

        this.getChildren().addAll(this.name, this.value);
    }
}

Without setPrefWidth() , my result now is the following: 没有setPrefWidth() ,我的结果如下:

Name1: 99999     Name3: 99999
Long Name: 99999 Name31: 99999
Name2: 99999     Longest3: 99999

The result I want to get at the end: 我想得到的结果是:

Name1:     99999   Name3:    99999
Long Name: 99999   Name31:   99999
Name2:     99999   Longest3: 99999

How can I achieve this? 我该如何实现?

You can use a GridPane to lay out the data like a table. 您可以使用GridPane像表一样布置数据。 This will automatically adjust the cell widths to match the longest field. 这将自动调整像元宽度以匹配最长的字段。

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

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