是否有更简单(更轻松)的方式将文本置于“区域”中心。使用JavaFX 8? [英] Is there an easier (lighter) way to center text in a "zone" with JavaFX 8?

查看:133
本文介绍了是否有更简单(更轻松)的方式将文本置于“区域”中心。使用JavaFX 8?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,首先,我已经实现了我想要的,也就是说,在区域的中心显示一些文本。屏幕截图(右上角):

So, first things first, I have achieved what I wanted, and that is, display some amount of text in the center of a "zone". Screenshot (upper right corner):

现在,我遇到的问题是我如何实现这个...这是整个标签的FXML摘录:

Now, the problem I have is how I achieved this... This is the FXML extract for the whole tab:

<Tab text="Statistics" closable="false">
    <SplitPane orientation="VERTICAL" dividerPositions="0.5">
        <SplitPane orientation="HORIZONTAL" dividerPositions="0.5">
            <GridPane gridLinesVisible="false">
               <!-- snip -->
            </GridPane>
            <!-- HERE -->
            <BorderPane>
                <center>
                    <Label text="No information"/>
                </center>
            </BorderPane>
        </SplitPane>
        <TableView fx:id="stats">
            <!-- snip -->
        </TableView>
    </SplitPane>
</Tab>

正如您所看到的,为了实现直观应该简单的事情(在区域的中心),我必须创建一个 BorderPane 并在其中放置一个 Label <中心],[;我甚至不必指定标签上的文字应该居中......

As you can see, in order to achieve something which intuitively should be simple (display some text in the center of a zone), I had to create a BorderPane and put a Label in its <center>; and I didn't even have to specify that the text on the label should be centered...

我尝试过的其他事情是:

The other things I have tried are:


  • 标签本身:然后是 ScrollPane的分隔符会被彻底忽视;左侧会扩展到 Scene 的全宽(是吗?)减去分隔符的宽度加上标签的文本,更重要的是,文本将位于区域的顶部;

  • AnchorPane 括起来:虽然这确实会保留区域的大小,文本将显示在左上角而不是中心。

  • put the Label itself: then the divider of the ScrollPane would be blatanly ignored; the left side would expand to the full width of the Scene (is that it?) minus the width of the divider plus the text of the label, and what is more, the text would be at the top of the zone;
  • enclose it with an AnchorPane: while this would indeed keep the size of the zone, the text would appear on the top left corner and not in the center.

是否有比上述更简单的解决方案?

Is there an easier solution than the above?

推荐答案

使用 StackPane 而不是 BorderPane 。默认情况下,StackPane将使您放入其中的任何节点居中。对我来说,这是最简单,最直观的解决方案。

Use a StackPane rather than a BorderPane. By default a StackPane will center any node you put in it. To me, this is the simplest, most intuitive solution.

<StackPane>
    <Label text="No information"/>
</StackPane>

处理它的另一种方法是在标签上设置约束,以便标签展开以填充该区域内可调整大小的区域和中心:

Another way to handle it is to set constraints on the label itself so that the label will expand to fill a resizable area and center itself within the area:

<SplitPane dividerPositions="0.5" 
           prefHeight="400.0" 
           prefWidth="600.0" 
           xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8">
   <items>
      <StackPane />
      <Label alignment="CENTER" 
             maxHeight="1.7976931348623157E308" 
             maxWidth="1.7976931348623157E308" 
             text="Label" />
   </items>
</SplitPane>

如果是多行标签(例如它有 \ n 文本中的新行或可用宽度强制换行,因为你约束宽度并设置 wrapText =true),那么你需要一个在标签内居中多行文字的附加设置: textAlignment =true textAlignment alignment 不同;特别是, textAlignment 仅适用于多行控件。

If it is a multi-line label (e.g. it has \n new lines in the text or the available width forces a wrap because you constrain the width and set wrapText="true"), then you need an additional setting to center the multi-line text within the label: textAlignment="true". textAlignment is different from alignment; in particular, textAlignment applies only to multi-line controls.

这篇关于是否有更简单(更轻松)的方式将文本置于“区域”中心。使用JavaFX 8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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