【发布时间】: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有一个VBox和Label,但ListView不起作用。不加起来。 -
基本上我已经用VBox开发了很多,所以我宁愿不把它改成我不知道怎么用的listView
-
您需要发布minimal reproducible example。这些标签是如何添加的?
标签: java javafx scrollpane vbox