无法使用ScrollPane滚动充满标签的VBox

时间:2018-10-19 16:41:18

标签: java javafx scrollpane vbox

基本上列出要累加的标签列表。当它们到达底部时,它们越界(也必须在TextField的后面),并且无法滚动它。 如何使他们表现正常? 标签被添加到ScrollPane内的VBox中

有问题的图片: Here

SceneBuilder屏幕截图: Here

FXML文件:

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

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

<AnchorPane fx:id="root" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.view.GameUIController">
   <children>
      <SplitPane dividerPositions="0.6889632107023411" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <items>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
               <children>
                  <SplitPane dividerPositions="0.8737373737373737" orientation="VERTICAL" prefHeight="398.0" prefWidth="407.0">
                    <items>
                      <AnchorPane minWidth="0.0" prefWidth="160.0">
                           <children>
                              <ScrollPane fx:id="scrollPane" fitToHeight="true" fitToWidth="true" pannable="true" prefWidth="407.0" vvalue="1.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                                 <content>
                                    <VBox fx:id="box" minWidth="0.0" />
                                 </content>
                              </ScrollPane>
                           </children>
                        </AnchorPane>
                      <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
                           <children>
                              <TextField fx:id="textField" onAction="#enterPressed" prefHeight="20.0" AnchorPane.bottomAnchor="5.0" AnchorPane.leftAnchor="-5.0" AnchorPane.rightAnchor="5.0" />
                           </children>
                        </AnchorPane>
                    </items>
                  </SplitPane>
               </children>
            </AnchorPane>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
               <children>
                  <SplitPane dividerPositions="0.5" layoutY="44.0" orientation="VERTICAL" prefHeight="398.0" prefWidth="181.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                    <items>
                      <AnchorPane fx:id="inventoryPane" minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
                           <children>
                              <Label layoutX="48.0" layoutY="30.0" text="Inventory" textAlignment="CENTER" AnchorPane.leftAnchor="5.0" AnchorPane.rightAnchor="5.0" AnchorPane.topAnchor="5.0">
                                 <font>
                                    <Font size="22.0" />
                                 </font>
                              </Label>
                           </children>
                        </AnchorPane>
                      <AnchorPane fx:id="playerPane" minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
                           <children>
                              <Label text="Player" textAlignment="CENTER">
                                 <font>
                                    <Font size="22.0" />
                                 </font>
                              </Label>
                           </children>
                        </AnchorPane>
                    </items>
                  </SplitPane>
               </children>
            </AnchorPane>
        </items>
      </SplitPane>
   </children>
</AnchorPane>

标签添加方法在另一个线程中(由于游戏机制的原因)

@FXML
    public void text(String text1) {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                Label label = new Label(text1);
                box.getChildren().add(label);

            }
        });
        listener = false;
        // scroll to bottom

    }

2 个答案:

答案 0 :(得分:0)

从fxml fitToHeight="true"中删除此代码,它很不错。

答案 1 :(得分:0)

显然我的GameEngine类看不到正确的节点,因此我将它们从控制器传递到引擎方法中。现在像魅力一样工作

相关问题