问题描述
我正在寻找 LayoutManager
,这将允许我在列中显示容器(例如JPanel)中的一组组件,大致如下所示。
I'm looking for a LayoutManager
that will allow me to show a set of components in a container (e.g. a JPanel), in columns, roughly as follows.
- 容器的宽度是输入约束。
- 容器的首选高度是宽度,并且是:
- 当在多个列中布置时,使组件适合宽度所需的最小高度。
- 使组件适合单列所需的最小高度。
这是一个例子(低科技文本)显示)显示13个组件。如果它们可以放在3列中,它们将如下所示:
Here's an example (low-tech text display) showing 13 components. If they can fit in 3 columns, they would look like this:
Foo1 Foo6 Foo11 Foo2 Foo7 Foo12 Foo3 Foo8 Foo13 Foo4 Foo9 Foo5 Foo10
如果太宽,它们看起来像这样:
If that's too wide, they'd look like this:
Foo1 Foo8 Foo2 Foo9 Foo3 Foo10 Foo4 Foo11 Foo5 Foo12 Foo6 Foo13 Foo7
如果它太宽了,它们看起来像这样:
And if that's too wide, they'd look like this:
Foo1 Foo2 Foo3 Foo4 Foo5 Foo6 Foo7 Foo8 Foo9 Foo10 Foo11 Foo12 Foo13
我是否可以使用预先存在的LayoutManager或子类来执行此类操作?
Is there a pre-existing LayoutManager that I can use or subclass to do something like this?
编辑:这非常接近,但该布局的组件高度是约束,而我的组件宽度是约束。
edit: this is very close to this other question about vertical layout, but that layout has the component height being the constraint, and mine has the component width being a constraint.
推荐答案
承认这种布局的另一个组件是,其中包含
VERTICAL_WRAP
,表示报纸样式布局,细胞垂直流动,然后水平流动。根据您的需要,提供一个合适的ListCellRenderer
,提到,可能就够了。One other component that admits this kind of layout is
JList
, which includes aVERTICAL_WRAP
that "Indicates a newspaper style layout with cells flowing vertically then horizontally." Depending on your needs, a suitableListCellRenderer
, mentioned here, may be suffice.这篇关于Swing:列流布局管理器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!