JavaFX TilePane不包装文本或显示省略号字符串

时间:2016-11-13 09:18:27

标签: java layout javafx

我正在使用TilePane,其中包含GridPane,我使用SceneBuilderFXML创建了它。问题是TilePane有效标题是这样说很奇怪:

当文本太大时我的布局有严重问题导致TilePane 没有包装文本或显示省略号字符串所以当我调整窗口大小时所有布局都没有进入应用程序

为什么TilePane会发生这种情况,并且在BorderPane内无法正常工作?

截图:

enter image description here

这是fxml代码:

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

<?import javafx.scene.control.ContextMenu?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.control.Tooltip?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.StackPane?>

<fx:root styleClass="smartController" type="StackPane" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <BorderPane fx:id="mainBorder" style="-fx-background-color: transparent; -fx-border-color: red;">
         <top>
            <TitledPane fx:id="titledPane" alignment="CENTER" contentDisplay="CENTER" graphicTextGap="0.0" text="I am The TilePane and i don't like wrapping the text!" wrapText="true" BorderPane.alignment="CENTER">
               <content>
                  <GridPane fx:id="topGrid" gridLinesVisible="true" hgap="5.0" style="-fx-background-color: rgb(0,0,0,0.85);;">
                    <columnConstraints>
                      <ColumnConstraints hgrow="SOMETIMES" percentWidth="70.0" />
                      <ColumnConstraints hgrow="SOMETIMES" minWidth="40.0" percentWidth="15.0" />
                        <ColumnConstraints hgrow="SOMETIMES" percentWidth="15.0" />
                    </columnConstraints>
                    <rowConstraints>
                      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                    </rowConstraints>
                     <children>
                        <TextField fx:id="pageField" alignment="CENTER" maxHeight="-Infinity" maxWidth="65.0" minHeight="-Infinity" minWidth="-Infinity" prefHeight="25.0" prefWidth="50.0" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.valignment="CENTER">
                           <tooltip>
                              <Tooltip text="current page" />
                           </tooltip>
                           <contextMenu>
                              <ContextMenu />
                           </contextMenu>
                        </TextField>
                     </children>
                  </GridPane>
               </content>
            </TitledPane>
         </top>
      </BorderPane>
   </children>
</fx:root>

1 个答案:

答案 0 :(得分:2)

默认情况下TitledPane基于minWidth有一个text。如果父级要将节点的大小调整为较小的大小,则必须手动分配值:

<TitledPane minWidth="0" fx:id="titledPane" alignment="CENTER" contentDisplay="CENTER" graphicTextGap="0.0" text="I am The TilePane and i don't like wrapping the text!" wrapText="true" BorderPane.alignment="CENTER">
   ...
</TitledPane>

可以在TitledPaneMin Width)的检查员的布局部分进行设置。

至于实际包装文本:

TitledPane的标题部分根本不够大,默认情况下不允许多行。您可以使用css样式表更改此内容:

#titledPane>.title {
    -fx-pref-height: 50; /* increase available area */
    -fx-alignment: top-center;
    -fx-padding: 4 4 4 30; /* increase left padding to align text */
}

#titledPane>.title>.arrow-button {
    -fx-translate-x: -20; /* move back arrow, which is also subject to the padding */
}