java - ScrollPane 中的 JavaFX FlowPane,动态调整滚动窗格的内容和大小
问题描述
我目前有一个以 FlowPane 作为内容的 ScrollPane。FlowPane 当前初始化时没有子节点、固定宽度和首选项/最小高度(但没有最大高度)。
在运行时向 FlowPane 添加项目时(我单击一些 UI 元素并将某些内容添加到 FlowPane),ScrollPane 应调整其高度,以防对 FlowPane 的添加不再适合。
我不明白如何设置 flowPane 和 ScrollPane 的高度以使其正常工作 - 如果这是一开始的问题。目前,只要添加到 FlowPane 的内容不适合其初始高度,就会添加内容,但不可见。属于 ScrollPane 的滚动条从不调整它的高度 - 如果是这样,我可以进一步向下滚动并查看内容。
假设我有一个具有一些宽度和高度的 ScrollPane、一些视口宽度/高度和一个具有一些宽度/高度的 FlowPane - 我的最小/首选/最大尺寸应该是什么设置?如何使 scrollPane 调整其滚动条行为或使内容可见?
ScrollPane 的 setFitToHeight 已经设置为 true,这似乎没有改变任何东西。
import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.ScrollPane.ScrollBarPolicy;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
public class FlowPaneTest extends Application
{
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception
{
// borderPane rootPane
BorderPane borderPane = new BorderPane();
borderPane.setMinSize(600, 600);
borderPane.setPrefSize(600, 600);
borderPane.setMaxSize(600, 600);
// container for the two scrollPanes below
FlowPane flow = new FlowPane();
borderPane.setRight(flow);
// two scrollPanes, each should resize it's height (width should be fixed) if
// children are added beyond it's current height
ScrollPane top = new ScrollPane();
ScrollPane bottom = new ScrollPane();
FlowPane scrollPaneContent = new FlowPane();
top.setContent(scrollPaneContent);
bottom.setContent(scrollPaneContent);
flow.getChildren().add(top);
flow.getChildren().add(bottom);
borderPane.setOnMouseClicked(new EventHandler<Event>()
{
@Override
public void handle(Event event)
{
Label l = new Label("test");
l.setMinSize(100, 100);
l.setPrefSize(100, 100);
l.setMaxSize(100, 100);
scrollPaneContent.getChildren().add(l);
}
});
// size settings
int width = 300, height = 300;
top.setHvalue(0.5);
top.setMinViewportHeight(height);
top.setPrefViewportHeight(height);
top.setMinViewportWidth(width);
top.setPrefViewportWidth(width);
top.setHbarPolicy(ScrollBarPolicy.ALWAYS);
top.setFitToHeight(true);
top.setMinSize(width, height);
top.setPrefSize(width, height);
top.setMaxWidth(width);
scrollPaneContent.setMinSize(width, height);
scrollPaneContent.setPrefSize(width, height);
scrollPaneContent.setMaxWidth(width);
scrollPaneContent.setPrefHeight(height);
bottom.setMinSize(width, height);
bottom.setPrefSize(width, height);
bottom.setMaxWidth(width);
bottom.setHvalue(0.5);
bottom.setMinViewportHeight(height);
bottom.setPrefViewportHeight(height);
bottom.setMinViewportWidth(width);
bottom.setPrefViewportWidth(width);
bottom.setHbarPolicy(ScrollBarPolicy.ALWAYS);
top.setFitToHeight(true);
bottom.setFitToHeight(true);
// stage
Scene scene = new Scene(borderPane, 600.0, 600.0);
primaryStage.setScene(scene);
primaryStage.show();
}
}
解决方案
尝试给出ScrollPane
首选项高度和宽度并添加此行
scrollPane.setFitToWidth(true);
推荐阅读
- node.js - 错误:无法跨一对多查询属性 TypeORM
- azure - Terraform azurerm_key_vault access_policy 覆盖现有访问策略
- python - RLLIB PPO 算法中的神经网络输出
- javascript - 我是 call api 的新手,有人可以帮我解决这个问题。使用 javascript 调用 Api 链接缩短器
- node.js - 用于特定模式的 Apollo graphql typedef
- git - 推送到远程存储库上的 temp_branch 时如何解决 git push 错误?
- maven - 通过运行 Maven 任务使用 Freemarker 生成 txt 文件
- reactjs - 是否可以使用 css 模块使用具有不同 CSS 的相同 React 组件?
- python - 创建列表的嵌套循环始终在字符串中间创建新行
- javascript - 如何在 Javascript 中循环遍历嵌套对象并随后将值添加到 HTML 表格单元格?