使用FXML在VBox中包装标签文本 [英] Wrapping Label text in a VBox using FXML

查看:148
本文介绍了使用FXML在VBox中包装标签文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个JavaFX应用程序,我想创建一个包含2个长文本的屏幕。我不知道文本是提前的,它会在运行时被一些代码填充。

I'm writing a JavaFX application and I'd like to create a screen that contains 2 long pieces of text. I don't know what the text is ahead of time, it will be filled in by some code at run time.

为了做到这一点,我想我会做一个VBox有2个标签。我假设如果我不添加尺寸,标签将跨越VBox。由于文本很长,我希望它包装。

To do this I thought I'd make a VBox with 2 Labels. I assume that if I don't add dimensions, the Labels will span the VBox. Since the text is long I'd like it to wrap.

这是我尝试过的FXML:

Here's the FXML that I tried:

<VBox spacing="20" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Label fx:id="label1" text="Dummy Text" wrapText="true" />   
      <Label fx:id="label2" text="Dummy Text" wrapText="true" />  
   </children>
</VBox>

这不起作用,因为文本没有换行但只是偏离右侧的窗口。我需要什么才能让它发挥作用?

This doesn't work because the text doesn't wrap but simply goes off the right side of the window. What do I need to get this to work?

推荐答案

尝试设置prefWidth和prefHeight,因为你的标签不知道何时开始换行

Try to set prefWidth and prefHeight , since your label doesnt know when to start wrap

<VBox prefHeight="100.0" prefWidth="300.0"  ...

如果不起作用,请在HBox的母公司上试试。
你在使用场景构建器吗?我建议你使用它

If that does not work try it on parent contrainer of HBox. Are you using scene builder? I recommend you to use it

效果很好(自动生成):

Works well(its auto generated):

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="333.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1">
   <center>
      <VBox prefHeight="200.0" prefWidth="325.0" BorderPane.alignment="CENTER">
         <children>
            <Label text="Wrap text Wrap text Wrap text Wrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap text" wrapText="true" />
            <Label layoutX="10.0" layoutY="10.0" text="just textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust text" />
         </children>
      </VBox>
   </center>
</BorderPane>

这篇关于使用FXML在VBox中包装标签文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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