【问题标题】:Stage: Not resizing to Scene舞台:未调整到场景大小
【发布时间】:2021-07-21 15:25:07
【问题描述】:

我在舞台上有一个场景。场景的宽度为 337.0 像素。但是,当我将它添加到舞台时,舞台的大小为 337.6 像素,由于 0.6 像素的差异,在屏幕的右边缘留下了一个白色间隙。

我尝试使用stage.sizeToScene(),但这不起作用。我还尝试通过尝试stage.setWidth(337) 手动设置舞台的宽度。这不影响舞台的宽度,它仍然是 337.6。

我尝试了以下方法:stage.setWidth(337.0)。然后当我在控制台打印舞台宽度时,使用stage.getWidth(),打印的值为337.0,但实际上,0.6像素的白线仍然存在。

我尝试过以下操作:

stage.hide();
stage.show();

这行得通,它删除了白线,但它显示的窗口切换看起来很糟糕。有没有办法在不像上面那样切换窗口的情况下做同样的事情。

我们将不胜感激。

【问题讨论】:

  • 不要手动调整/定位节点,而是使用自动执行此操作的布局(AnchorPane 在这里并不是很有用) - 我怀疑这两种形式之间存在轻微的大小不匹配。跨度>
  • 是的,但是我也看到这个问题发生在我之前使用的其他布局中。 Swing 框架中也出现过这个问题,但我一直没有得到任何答案。
  • 好的,我发现了一些东西,请参考我的问题中的编辑
  • hmm .. 可能是舍入/初始阶段大小的问题:隐约记得(手边没有 id,抱歉)最近修复了一个问题。你看到的可能是剩下的..
  • 当我切换回注册场景时会发生什么,大小是我提到的 337,但最初它变成了 337.6。

标签: java javafx


【解决方案1】:

正如@wollsi 所建议的,我想对他的代码进行一些编辑。这对我来说很有效:

    public void setActiveScene(Scene newActiveScene) {
        stage.setScene(newActiveScene);
        stage.setWidth(newActiveScene.getRoot().minWidth(-1));
        stage.setHeight(newActiveScene.getRoot().minHeight(-1));
    }

【讨论】:

    【解决方案2】:

    在为我的舞台设置正确大小时,我遇到了类似的问题。

    对我来说有帮助的是使用:

    stage.setMinWidth(newActiveScene.getRoot().minWidth(-1));
    stage.setMinHeight(newActiveScene.getRoot().minHeight(-1));
    

    stage 是您正在使用的舞台,newActiveScene 是您在舞台上设置的新场景。

    实际上,我正在使用一种方法在舞台上设置新场景,如下所示:

    /**
     * Sets a new active {@link Scene} on the stage.
     *
     * @param newActiveScene the new scene to show
     */
    public static void setActiveScene(Scene newActiveScene) {
        stage.setScene(newActiveScene);
        stage.setMinWidth(newActiveScene.getRoot().minWidth(-1));
        stage.setMinHeight(newActiveScene.getRoot().minHeight(-1));
    }
    

    其中stage 是使用的舞台。这假定 stage.show() 已经被调用过一次(例如在初始化阶段时)。

    【讨论】:

    • 很抱歉您的代码不起作用,但是当我用 setWidth 和 setHeight 替换 setMinWidth 和 setMinHeight 时,它起作用了,谢谢。
    猜你喜欢
    • 2018-07-05
    • 2019-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多