JavaFX中的GridPane布局与自动换行文本

6

我正在使用JavaFX 8,尝试使用GridPage显示一组属性和值。但是某些值可能太长而无法放在一行上,我希望能将这些值自动换行,但似乎无法让网格面板在纵向上扩展以适应换行后的文本。我基本上希望看到类似HTML表格的行为。不确定是否应该使用JavaFX中的其他布局。

Property A    Value A
Property B    Value B
Property C    The Values for property C is so long
              that it wraps
Property D    Value D

非常感谢您的帮助。


你能发一些代码展示你是如何设置的吗? - James_D
这可以通过设置 VGrow 并使用 TextFlow 轻松实现。正如 James_D 所说,如果您展示您目前拥有的/尝试过的内容,那将会很有帮助。 - Itai
1个回答

4

这是使用SceneBuilder构建的:

small large

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<VBox prefWidth="220.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <GridPane hgap="20.0" vgap="10.0">
        <columnConstraints>
          <ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="-Infinity" />
          <ColumnConstraints hgrow="SOMETIMES" maxWidth="1.7976931348623157E308" minWidth="20.0" />
        </columnConstraints>
        <rowConstraints>
          <RowConstraints minHeight="-Infinity" valignment="TOP" vgrow="SOMETIMES" />
          <RowConstraints minHeight="-Infinity" valignment="TOP" vgrow="SOMETIMES" />
          <RowConstraints minHeight="-Infinity" valignment="TOP" vgrow="SOMETIMES" />
        </rowConstraints>
         <children>
            <Label text="Property A" />
            <Label text="Property B" GridPane.rowIndex="1" />
            <Label text="Property C" GridPane.rowIndex="2" />
            <Label text="Value A" GridPane.columnIndex="1" />
            <Label text="The value for property B is so long that it wraps" wrapText="true" GridPane.columnIndex="1" GridPane.rowIndex="1" />
            <Label text="Value C" GridPane.columnIndex="1" GridPane.rowIndex="2" />
         </children>
         <padding>
            <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
         </padding>
      </GridPane>
   </children>
</VBox>

2
谢谢提供这个工作示例,已经足够了。我一直在使用行约束的prefHeight属性来提供适当的间距,这导致文本无法换行。现在看起来很明显。 - Mike

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接