gpt4 book ai didi

Java LayeredPane LayoutManager add()方法冲突

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:38:33 26 4
gpt4 key购买 nike

假设我有一个名为 mainPanelJLayeredPane,它使用了 BorderLayout。我还有一个名为 backgroundLabelJLabel,它包含一个图像。我如何将 backgroundLabel 添加到 mainPanel 的底层?

mainPanel.add(backgroundLabel, new Integer(-1), new Integer(0));

上面这行似乎是显而易见的答案,如果 mainPanel 使用的是空布局,它也可以工作。 mainPanel 中的 BorderLayout 不喜欢该命令并提供以下堆栈跟踪。

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string (or null)
at java.awt.BorderLayout.addLayoutComponent(Unknown Source)
at java.awt.Container.addImpl(Unknown Source)

如何将 backgroundLabel 添加到 mainPanel 的底层而不与 BorderLayout 冲突?

最佳答案

How to Use Layered Panes: Laying Out Components in a Layered Pane 中所述,“Java 平台提供的所有布局管理器都将组件排列成好像它们都在一层上一样。”您已指定 BorderLayout。您调用add()调用 addImpl(java.awt.Component, java.lang.Object, int) .因为 BorderLayout 实现了 LayoutManager2,所以 constraints 参数的值必须是为 BorderLayout 定义的 String 约束。 ,而不是具有值 -1Integer,例如

mainPanel.add(backgroundLabel, BorderLayout.SOUTH, 0);

My intent is for the backgroundLabel to be added to it's own bottom layer behind all other components.

JLayeredPane 上设置布局使“组件就像都在一层上一样”。相反,在占据最深层的组件上设置布局,并将标签添加到该组件。在这个example ,标签和按钮被添加到每个层的 JPanel 具有默认的 FlowLayout

关于Java LayeredPane LayoutManager add()方法冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37376915/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com