【问题标题】:Can't scroll VBox full of labels using ScrollPane无法使用 ScrollPane 滚动充满标签的 VBox
【发布时间】:2018-10-19 16:41:17
【问题描述】:

基本上制作一个加起来的标签列表。当它们到达底部时,它们会超出范围(也必须在 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

    }

【问题讨论】:

  • 使用ListView
  • 我之前选择了那个选项,但它会导致太多问题(无法在 ListView 中添加更多项目)所以推荐使用 VBox
  • 无法在ListView 中添加更多项目?这意味着什么?你的ScrollPane 有一个VBoxLabel,但ListView 不起作用。不加起来。
  • 基本上我已经用VBox开发了很多,所以我宁愿不把它改成我不知道怎么用的listView
  • 您需要发布minimal reproducible example。这些标签是如何添加的?

标签: java javafx scrollpane vbox


【解决方案1】:

从 fxml fitToHeight="true" 中删除它,祝你好运。

【讨论】:

  • 它改变的只是滚动条的大小。问题仍然存在 *编辑:它现在也在上下移动,但 vbox 没有移动
  • 那么有些东西您没有向我们展示,如果您不发布 fxml 和添加标签的相关代码,我将无能为力。所以我已经完成了帮助,直到那时@me 在评论中,当你这样做时我会提供帮助
  • 现在已经完成了。如果您还需要了解任何其他信息,请告诉我
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2016-08-03
    • 2017-11-09
    • 1970-01-01
    • 2015-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多