BorderLayout - 防止“居中”被“切断”的部件
我有一个包含两个 JLabel 的 JPanel。该面板使用 BorderLayout。
一个 JLabel 放置在 BorderLayout.CENTER
位置,另一个放置在 BorderLayout.PAGE_END
位置。
当我调整面板大小,使其没有足够的垂直空间来显示两个标签时,居中的标签始终会被 PAGE_END
位置的标签覆盖(截断)。
由于居中标签中显示的信息比其他标签更重要,因此我希望居中标签重叠(或切断)其下方的标签。
似乎 BorderLayout (以及 GridBagLayout )总是从“从上到下”绘制组件,而那些“稍后”绘制的组件将覆盖之前绘制的组件。
有什么方法可以说服 BorderLayout (或任何其他 LayoutManager)假设某个组件应该始终位于“顶部”?
我尝试使用
panel.setComponentZOrder(label1, 1);
panel.setComponentZOrder(label2, 0);
但这没有什么区别。
I have a JPanel that contains two JLabel. The panel uses a BorderLayout.
One JLabel is put into BorderLayout.CENTER
position, the other in BorderLayout.PAGE_END
When I resize the panel so that it does not have enough vertical space to show both labels, the centered label is always overwritten (cut off) by the label in the PAGE_END
position.
As the information displayed in the centered label is more important than the other, I would like the centered label to overlap (or cut off) the label below it.
It seems that BorderLayout (and GridBagLayout as well) always paints the components from "top to bottom" and those that are painted "later" will overwrite the ones painted before.
Is there some way I can convince BorderLayout (or any other LayoutManager) to assume that a certain component should always be "at the top"?
I tried using
panel.setComponentZOrder(label1, 1);
panel.setComponentZOrder(label2, 0);
but that didn't make a difference.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种方法是使用符合首选尺寸的
GridLayout
自定义变体。PreferredSizeGridLayout
使用PreferredBoundable
实现就是一个示例。附录:这是我尝试过的测试代码。如果不进行更改,下部标签将“滑动”到上部标签下方,但您必须处理
horizontalAlignment
属性。One approach would be to use a custom variation of
GridLayout
that respects preferred sizes.PreferredSizeGridLayout
using thePreferredBoundable
implementation is an example.Addendum: Here's the test code I tried. Without change, the lower label "slides" beneath the upper, but you'll have to handle the
horizontalAlignment
property.调用
setMinimumSize(Dimension)
。Call
setMinimumSize(Dimension)
on the top-level container when there is enough text in the center label.