Java LayeredPane LayoutManager add() 方法冲突

Java LayeredPane LayoutManager add() Method Conflict

假设我有一个名为 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, "All of the layout managers provided by the Java platform arrange the components as if they were all on one layer." You have specified BorderLayout. Your call to add() invokes addImpl(java.awt.Component, java.lang.Object, int). Because BorderLayout implements LayoutManager2, your value for the constraints parameter must be a String constraint defined for BorderLayout 中所述,不是具有值 -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 上设置布局使得 "the components as if they were all on one layer." 相反,在占据最深层的组件上设置布局,并将标签添加到该组件。在这个 example 中,标签和按钮被添加到具有默认值 FlowLayout 的每个图层的 JPanel