保持比例的情况下调整JavaFX窗口大小?

4

我有一个窗口,需要保持正方形。我的代码是

 primaryStage.minHeightProperty().bind(scene.widthProperty());
 primaryStage.minWidthProperty().bind(scene.heightProperty());

当我尝试缩小正方形的大小时,它确实会进行调整,但是存在问题。有时一个边会比另一个边稍微短或长一些。有没有解决方法?我当前使用的代码是否有问题?

1个回答

1
这并没有直接回答你的问题,但我认为这实际上是更好的用户体验,因为你不会限制用户。不要固定舞台的宽高比,而是考虑使用插图来填充你想要保持方形的内容。
private val DEF_PAD = 6.0
chart.paddingProperty.bind(boundValue(stage.widthProperty, stage.heightProperty) {
    // Maintain a square plot with padding
    val w = stage.getWidth
    val h = stage.getHeight
    val extra = DEF_PAD + 0.5 * Math.abs(w - h)
    if (w > h) {
        new Insets(DEF_PAD, extra, DEF_PAD, extra)
    } else if (h > w) {
        new Insets(extra, DEF_PAD, extra, DEF_PAD)
    } else {
        new Insets(DEF_PAD)
    }
})

(使用Scala编写。boundValue只是一个实用程序,它创建具有varargs依赖项列表的绑定)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接