有没有办法将文本字段包装在网格窗格中?

时间:2019-01-03 10:06:49

标签: java javafx

我正在研究javafx游戏项目,目的是找到一个组合,我的问题是关于Ihm,如果文本太多,则文本会超出网格范围

有没有一种方法可以将文本包装在网格窗格中,或者我必须更改xml文件?

https://imgur.com/YMe0eTH

https://imgur.com/CU8cjQ0

我的xml文件:

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>

<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?><BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="500.0" prefWidth="350.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.joffrey.controller.game.FindTheCombinationGameController">
    <center>
        <GridPane BorderPane.alignment="CENTER">
            <columnConstraints>
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            </columnConstraints>
            <rowConstraints>
                <RowConstraints maxHeight="450.0" minHeight="10.0" prefHeight="155.0" vgrow="SOMETIMES" />
            <RowConstraints maxHeight="255.0" minHeight="-Infinity" prefHeight="245.0" vgrow="SOMETIMES" />
                <RowConstraints maxHeight="-Infinity" minHeight="-Infinity" prefHeight="100.0" vgrow="SOMETIMES" />
            </rowConstraints>
            <children>
                <HBox fx:id="activeGameBox" maxHeight="-Infinity" maxWidth="-Infinity" GridPane.halignment="CENTER" GridPane.rowIndex="2" GridPane.valignment="CENTER">
                    <children>
                        <TextField fx:id="gameProposalInputTxt" prefWidth="200.0">
                            <HBox.margin>
                                <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                            </HBox.margin>
                        </TextField>
                        <Button fx:id="sendCombinationBtn" mnemonicParsing="false" onAction="#onSendCombinationAction" prefWidth="80.0" text="Enter">
                            <HBox.margin>
                                <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                            </HBox.margin>
                        </Button>
                    </children>
                    <GridPane.margin>
                        <Insets bottom="10.0" />
                    </GridPane.margin>
                </HBox>
            <Text fx:id="gameTextArea" strokeType="OUTSIDE" strokeWidth="0.0" text="gameTextArea" textAlignment="CENTER" GridPane.halignment="CENTER">
               <font>
                  <Font size="17.0" />
               </font></Text>
            <HBox fx:id="endGameBox" maxHeight="-Infinity" maxWidth="-Infinity" visible="false" GridPane.halignment="CENTER" GridPane.rowIndex="2">
               <children>
                  <Button fx:id="restartGameBtn" maxHeight="-Infinity" maxWidth="-Infinity" mnemonicParsing="false" onAction="#onRestartGameAction" prefHeight="50.0" prefWidth="130.0" text="Restart game">
                     <HBox.margin>
                        <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                     </HBox.margin>
                  </Button>
                  <Button fx:id="quitGameBtn" maxHeight="-Infinity" maxWidth="-Infinity" mnemonicParsing="false" onAction="#onQuitGameAction" prefHeight="50.0" prefWidth="130.0" text="Quit">
                     <HBox.margin>
                        <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                     </HBox.margin>
                  </Button>
               </children>
            </HBox>
            <GridPane GridPane.rowIndex="1">
              <columnConstraints>
                  <ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="10.0" prefWidth="80.0" />
                <ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="10.0" prefWidth="140.0" />
                <ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="10.0" prefWidth="140.0" />
              </columnConstraints>
              <rowConstraints>
                  <RowConstraints minHeight="10.0" prefHeight="30.0" valignment="CENTER" vgrow="SOMETIMES" />
              </rowConstraints>
               <children>
                  <Text fx:id="gameTurnDisplay" lineSpacing="2.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Turn" textAlignment="CENTER" GridPane.halignment="CENTER" GridPane.valignment="BOTTOM" />
                  <Text fx:id="gameConvertedResultDisplay" lineSpacing="2.0" strokeType="OUTSIDE" strokeWidth="0.0" text="+-Result" textAlignment="CENTER" GridPane.columnIndex="2" GridPane.halignment="CENTER" GridPane.valignment="BOTTOM" />
                  <Text fx:id="gameProposalDisplay" lineSpacing="2.0" strokeType="OUTSIDE" strokeWidth="0.0" text="proposal" textAlignment="CENTER" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.valignment="BOTTOM" />
               </children>
            </GridPane>
            </children>
        </GridPane>
    </center>
</BorderPane>

1 个答案:

答案 0 :(得分:0)

我不确定我是否理解您的问题,但是如果您希望文本始终适合表格,可以尝试以下操作:

  1. 如果对于网格来说文本总是很大,则可以使用较小的字体。

    gameTextArea.setStyle("-fx-font-size: 20;");
    
  2. 如果文本发生更改,则可以在文本的textProperty中添加一个ChangeListener,并测试new_value.length()是否大于设置的值,以及是否更改字体大小。

    gameTextArea.textProperty().addListener((observable, old_value, new_value) -> {
        if(new_value.length() > value) {
            //font size smaller
        }
    });
    
  3. 如果要使其准确,可以计算文本的像素宽度,然后根据宽度更改字体大小:

    gameTextArea.textProperty().addListener((observable, old_value, new_value) -> {
        Text text = new Text(currText);
        text.setFont(tf.getFont()); 
        double width = text.getLayoutBounds().getWidth()
        if(width > value) {
            //font size smaller
        }
    });