gpt4 book ai didi

JavaFX:滚动 Pane 不适合父级

转载 作者:搜寻专家 更新时间:2023-11-01 00:54:33 24 4
gpt4 key购买 nike

我想创建以下场景:

+-----------------------------------------------+
|ROOT (=BorderPane) |
| |
| +-------------------------------------------+ |
| |TOOL BAR | |
| |(north) | |
| +-------------------------------------------+ |
| |
| +-------------------------------------------+ |
| |MyConfigurationComponent extends BorderPane| |
| |(center) | |
| | +-------------------------+ +---------+ | |
| | |ScrollPane | | | | |
| | |(center) | | | | |
| | | +---------------------+ | | | | |
| | | | | | | | | |
| | | | | | | SIDE | | |
| | | | CANVAS | | | PANE | | |
| | | | | | | (right) | | |
| | | | | | | | | |
| | | | | | | | | |
| | | +---------------------+ | | | | |
| | | | | | | |
| | +-------------------------+ +---------+ | |
| | | |
| +-------------------------------------------+ |
| |
+-----------------------------------------------+

期待

我想让 ScrollPane 尽可能大。这意味着无论场景大小如何,ScrollPane 都应该增长并填满整个区域。

因为 TOOL BARSIDE PANE 都有固定的(首选)宽度和高度,我认为 ScrollPane 会拉伸(stretch)和填充整个剩余区域。

实际行为

但事实并非如此。 ScrollPane 以某种方式神奇地将其最小尺寸设置为大约 40x40。高度被拉伸(stretch)以匹配 SIDE PANEL 的高度,但宽度仍然相同,似乎无法改变它。

事实上,使 ScrollPane 变大的唯一一件事就是明确地将 minWidth 设置为某个值(即 400)。但这并没有解决有关 ScrollPane 的自动调整大小的问题。

当我将背景颜色设置为 MyConfigurationComponent 时,我可以看到背景颜色按预期填满了整个区域(因此问题不在于 MyConfigurationComponent)。看起来它的中心子项 (ScrollPane) 呈现为左侧,其右侧子项 (SIDE PANEL) 呈现为中心,因为右侧的所有剩余区域都保持空白 (但有了背景,所以它仍然是 MyConfigurationComponent!)。

我尝试了什么

  • ScrollPaneminprefmax 大小设置为特定值
  • 将首选尺寸绑定(bind)到父尺寸
  • fitToWidthfitToHeight 设置为 true
  • AnchorPane 包裹 ScrollPane,所有四个边都有 anchor
  • 用 VBox 包装 ScrollPane 并设置优先级为 allways

当前的解决方法

MyConfigurationComponent 的宽度减去 SIDE PANEL 的宽度绑定(bind)到 ScrollPane。在很多方面都是错误的。

代码

根文件.fxml

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="cz.upol.fapapp.cfa.gui.frame.FxCFAConfigFrameController">
<top>
<ToolBar>
<Button text="Some button" />
</ToolBar>
</top>
<center>
<MyConfigurationComponent fx:id="configComp" />
</center>
</BorderPane>

我的配置组件.fxml

<BorderPane xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1">
<center>
<ScrollPane fx:id="scrollPane">
<content>
<Canvas width="2000" height="2000" />
</content>
</ScrollPane>
</center>
<right>
<VBox prefWidth="100.0" BorderPane.alignment="CENTER_RIGHT">
<children>
<Label text="Colors" />
<ComboBox fx:id="cmbColors" prefWidth="100.0" />
<!-- ... -->
<Separator prefHeight="15.0" prefWidth="100.0" />
<Button mnemonicParsing="false" text="Action 1" />
<Button mnemonicParsing="false" text="Action 2" />
</children>
</VBox>
</right>
</BorderPane>

问题

  • BorderPane 的中心扩展了它的大小来填充内容,不是吗?
  • 这是否是由 BorderPane 内部的 BorderPane 以某种方式引起的?
  • 有比“我的解决方法”更好的解决方案吗?

最佳答案

您可以将其放入您的代码中,在 start 方法内:

stage.widthProperty().addListener(event -> {
scrollPane.setPrefWidth(stage.getWidth());
});

stage.heightProperty().addListener(event -> {
scrollPane.setPrefHeight(stage.getHeight());
});

如果这不起作用,我建议也将 BorderPane 的宽度和高度设置为舞台的尺寸:

stage.widthProperty().addListener(event -> {
scrollPane.setPrefWidth(stage.getWidth());
borderPane.setPrefWidth(stage.getWidth());
});

stage.heightProperty().addListener(event -> {
scrollPane.setPrefHeight(stage.getHeight());
borderPane.setPrefHeight(stage.getHeight());
});

关于JavaFX:滚动 Pane 不适合父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45988053/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com