gpt4 book ai didi

java - 如何设置 JavaFX ScrollPane 的最大大小并使滚动条仅在需要时出现?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:55:02 24 4
gpt4 key购买 nike

我正在尝试构建一个单窗口应用程序以更好地了解 JavaFX。这非常好,也很容易,直到我没有深入细节......

我有一个 AnchorPane 作为其他 GUI 元素的主要容器。我意识到,这对于我的笔记本电脑屏幕来说太高了(805 像素高,600 像素宽),所以我决定在缩小窗口时将 AnchorPane 放在 ScrollPane 中以具有滚动条。AnchorPane在FXML中配置,ScrollPane在Java源码中配置。

AnchorPane:

<AnchorPane maxHeight="805.0" prefHeight="805.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.jzoli.mp3checker.view.MainWindowController">

...

滚动面板:

public class ScrollableMainFrame extends ScrollPane {

public ScrollableMainFrame(Pane content) {
super();

// set scrollbar policy
this.setHbarPolicy(ScrollBarPolicy.AS_NEEDED);
this.setVbarPolicy(ScrollBarPolicy.AS_NEEDED);

// set the main window in the scroll pane
this.setContent(content);
}

然后我加载 FXML,将 AnchorPane 放入 Scrollpane 中,并让它显示:

private final void initWindow() {
try {
// Load main window layout from fxml file.
URL mainWindowURL = MainApp.class.getResource("view/MainWindow.fxml");
FXMLLoader loader = new FXMLLoader(mainWindowURL, guiLabels);
mainWindow = (AnchorPane) loader.load();
MainWindowController controller = loader.getController();
controller.setMainAppAndGUILabels(this);

// create a scrollable Pane, and put everything inside
scrollableMainFrame = new ScrollableMainFrame(mainWindow);

// Show the scene containing the layout.
Scene scene = new Scene(scrollableMainFrame);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
LOG.error("Error loading GUI!", e);
}
}

到目前为止一切顺利,窗口出现并且没有滚动条,直到我不缩小它。但我想最大化我的窗口,因为让它变大没有意义(AnchorPane 有固定大小),只有更小。我已经想通了,必须设置 PrimaryStage 的 Max size 来限制实际窗口,限制 ScrollPane 没有效果。

问题来了:如果我想为 PrimayStage 设置 MaxHeight 和 MaxWidth,我只会得到不需要的结果。如果我希望我的 PrimaryStage 具有与 Anchorpane 相同的最大大小,则窗口要么不显示,要么有滚动条!

如果我将这一行放在我的 InitWindow 方法中

        // Show the scene containing the layout.
Scene scene = new Scene(scrollableMainFrame);
primaryStage.setScene(scene);
// set max window size
primaryStage.setMaxHeight(scrollableMainFrame.getHeight());
primaryStage.show();

什么都不会出现,因为显然'可滚动的主框架'在这一点上没有高度。

如果我将 setMaxHeight() 放在末尾,例如

    primaryStage.setScene(scene);
primaryStage.show();
// set max window size
primaryStage.setMaxHeight(scrollableMainFrame.getHeight());

然后最大高度将被有效设置,但滚动条出现并保持可见,即使窗口是全尺寸的!

有谁知道为什么,以及如何设置我的窗口的最大尺寸,而不是始终打开滚动条?

(简单地将数字添加到最大值,如 primaryStage.setMaxHeight(scrollableMainFrame.getHeight() + 15);
什么也没做,滚动条还在!)

最佳答案

谢谢,James_D,您引导我找到了解决方案!

确实如你所说,出现了滚动条,因为PrimaryStage也有标题栏,我忘记了。这让我想到:如何根据内容计算窗口的完整大小,将其设置为最大大小?好吧,我不需要!逻辑有点扭曲,但有效:我只需向 Primarystage 询问其实际大小,并将其设置为最大值。诀窍是,我需要在创建窗口后执行此操作:

        // create the window
primaryStage.show();
// set actual size as max
primaryStage.setMaxHeight(primaryStage.getHeight());
primaryStage.setMaxWidth(primaryStage.getWidth());

关于java - 如何设置 JavaFX ScrollPane 的最大大小并使滚动条仅在需要时出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35271024/

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