JavaFX布局问题

发布于 2024-08-28 04:33:45 字数 853 浏览 9 评论 0原文

我在理解 JavaFX 中的布局时遇到一些问题。考虑以下代码。

Stage {
    title: "ListView test"
    scene: Scene {
        width: 400
        height: 400
        content: [
            VBox {
                content: [
                    ListView {
                        height: 200
                        width: 200
                        items: ["item1", "item2"]
                    }
                ]
            }
        ]
    }
}

我期待 ListView 以 200 x 200 尺寸显示,但无论我如何尝试解决此问题,ListView 的宽度和高度似乎都是固定的。但下面的代码可以按照我的意图显示 ListView 。

Stage {
    title: "ListView test"
    scene: Scene {
        width: 400
        height: 400
        content: [
            ListView {
                height: 200
                width: 200
                items: ["item1", "item2"]
            }
        ]
    }
}

那么,这里有什么问题呢?我无法在布局中使用 ListView?

I am having some problem understanding layouts in JavaFX. Consider following code.

Stage {
    title: "ListView test"
    scene: Scene {
        width: 400
        height: 400
        content: [
            VBox {
                content: [
                    ListView {
                        height: 200
                        width: 200
                        items: ["item1", "item2"]
                    }
                ]
            }
        ]
    }
}

I was expecting ListView showing up in 200 x 200 dimension but no matter how I tried to fix this, the width and height of ListView seemed fixed. But following code works for showing ListView as I intended.

Stage {
    title: "ListView test"
    scene: Scene {
        width: 400
        height: 400
        content: [
            ListView {
                height: 200
                width: 200
                items: ["item1", "item2"]
            }
        ]
    }
}

So, what is the problem here? I cannot use ListView within layouts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏璃陌 2024-09-04 04:33:45

在 JavaFX 1.2.x 中,布局中节点的宽度和高度信息用于读取目的。如果您想指定“首选”尺寸,请使用

ListVIew {
    layoutInfo: LayoutInfo {width: 200, height: 200}
}

这将在布局中作为首选尺寸进行处理,并且组件将根据此进行布局。如果您有使用 NetBeans JavaFX Composer Preview 的经验,您可能会注意到生成的代码使用特殊类型的绑定,允许在布局和其他容器(场景、组)中正确调整大小。

广告损坏:Java FX 中的布局是在 Node 上构建的,并使用算法方法来定位和调整组件大小,该方法独立于组/场景中使用的几何位置。不幸的是,在 1.2 中它带来了某种二元性,这应该会误导初学者。

In JavaFX 1.2.x the width and height information on node in layout is for reading purpose. If you want to specify "preferred" dimension use

ListVIew {
    layoutInfo: LayoutInfo {width: 200, height: 200}
}

This will be processed in layout as preferred size and component will be layout-ed according to this. If you have experience with NetBeans JavaFX Composer Preview you may noticed that generated code use special kind of binding witch allows correct sizing in layout and other containers (Scene, Group).

Ad Broken: The layouts in Java FX are build over Node and uses algorithmic approach to positioning and resizing components which is independent to geometrical position used in Group/Scene. Unfortunately in 1.2 it brings some sort of duality which should be misleading for beginners.

清浅ˋ旧时光 2024-09-04 04:33:45

布局节点可以更改其子节点的位置和大小。简单地设置子级的宽度和高度并期望布局知道它应该尊重您的新值是不好的,您需要的是某种方式来告诉布局节点您想要尊重的值。看一下 LayoutInfo 类,以及 Node.js 中的layoutInfo 变量。

Layout nodes are allowed to change the position and size of their children. Simply setting a child's width and height and expecting the layout to know it's supposed to respect your new values is no good, what you need is some way to tell the layout node the values you'd like to respect. Take a look at the LayoutInfo class, and the layoutInfo variable in Node.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文