为什么添加新组件时 BorderLayout 会覆盖组件?

发布于 2024-09-27 13:07:23 字数 700 浏览 2 评论 0原文

BorderLayout 做了一些奇怪的事情。如果我将两个面板添加到具有相同约束的 Container 中(例如 BorderLayout.CENTER),那么第一个面板就会消失,即使第二个面板被删除或创建不可见

将每个元素“堆叠”在前面的元素之上似乎是有意义的。

这是正确的并且是有意设计的吗?如果是这样,有相关文档吗?

还有其他人为此感到沮丧吗?您有解决方案吗,例如自定义 LayoutManager

示例代码:

JFrame frame = new JFrame();
frame.setSize(500, 500);

JPanel panel1 = new JPanel();
panel1.setBackground(Color.blue);
frame.getContentPane().add(panel1);

JPanel panel2 = new JPanel();
panel2.setBackground(Color.red);
frame.getContentPane().add(panel2);

panel2.setVisible(false); // Seems like it should allow us to see panel1.

frame.setVisible(true);

这将创建并显示一个 500x500 的空白框。

BorderLayout does something strange. If I add two panels to a Container with the same constraint (BorderLayout.CENTER for instance), then the first one goes away, even if the second one is deleted or made invisible

It seems as though it would make sense for it to "stack" each element on top of the previous ones.

Is this correct and by design? If so, is there some documentation on it?

Has anyone else been frustrated by it? Have you a solution, such as a custom LayoutManager?

Sample code:

JFrame frame = new JFrame();
frame.setSize(500, 500);

JPanel panel1 = new JPanel();
panel1.setBackground(Color.blue);
frame.getContentPane().add(panel1);

JPanel panel2 = new JPanel();
panel2.setBackground(Color.red);
frame.getContentPane().add(panel2);

panel2.setVisible(false); // Seems like it should allow us to see panel1.

frame.setVisible(true);

This creates and displays a 500x500 blank box.

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

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

发布评论

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

评论(2

捶死心动 2024-10-04 13:07:23

BorderLayout 根本就不是为了做你想做的事情而设计的。责任分离。如果您想要这种行为,您应该编写:将 BorderLayoutCardLayout 组合起来。尽管对于实际的堆栈行为,您必须自己编写一些代码(或者找到已经编写过代码的人。)

BorderLayout was simply not designed to do what you want. Separation of responsibility. If you want that behavior you should compose: combine the BorderLayout with a CardLayout. Though for the actual stack behavior, you'll have to code something yourself (or find someone who already has.)

缺⑴份安定 2024-10-04 13:07:23

这是正确的并且是有意设计的吗?

是的。

您需要了解布局管理器工作原理的基础知识。布局管理器的工作之一是设置添加到面板的组件的“位置”和“大小”。对于 BorderLayout,它仅跟踪 5 个组件,因此布局管理器只知道添加到 CENTER 的最后一个组件。

将组件添加到面板时,布局管理并未完成。当框架被打包或变得可见(或调用 revalidate() 方法)时,就会完成此操作。在这种情况下,蓝色面板不是由 BorderLayout 管理的组件的一部分,因此其大小保持为 (0, 0),这意味着没有任何内容可绘制。

尝试将代码更改为:

JPanel panel1 = new JPanel();
panel1.setSize(200, 200);

,您将看到以指定尺寸绘制的蓝色面板。

现在尝试注释掉:

//panel2.setVisible(false); 

,您将看到两个面板。这是因为当组件添加到面板时,它们会被分配一个 ZOrder。基本上最后添加的组件首先被绘制,这就是为什么蓝色面板被绘制在红色面板的顶部。查看 Container 类的 setComponentZOrder() 方法以获取更多信息。

CardLayout 可能是您应该使用的布局管理器,但您可以查看 重叠布局也是如此。

Is this correct and by design?

Yes.

You need to understand the basics of how layout managers work. One of the jobs of the layout manager is to set the "location" and "size" of the components added to the panel. In the case of a BorderLayout it only tracks 5 components so only the last component added to the CENTER is known by the layout manager.

Layout management is not done when components are added to the panel. It is done when the frame is packed, or made visible (or the revalidate() method is invoked) . In this case the blue panel is not part of the components managed by the BorderLayout so its size remains (0, 0), which means there is nothing to paint.

Try changing your code to:

JPanel panel1 = new JPanel();
panel1.setSize(200, 200);

and you will see the blue panel painted at the specified size.

Now try commenting out:

//panel2.setVisible(false); 

and you will see both panels. This is because as components are added to the panel they are assigned a ZOrder. Basically the last component added is painted first, which is why the blue panel is painted on top of the red panel. Check out the setComponentZOrder() method of the Container class for more information.

The CardLayout is probably the layout manager you should be using, but you can check out the Overlap Layout as well.

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