如何使 JPanel 位于另一个已设置大小的 JPanel 之上

How to make a JPanel stay on top of another JPanel that has setSized

我查看了其他 Whosebug 和许多其他使用 JLayeredPane 的示例,但我无法解决我的问题。

Frame class -> Main JPanel class -> 2 JPanel classes

我将 Main JPanel 设置为 Border 布局,并且在 Main JPanel 中将 2 个 JPanel 设置为 North 和 South。 North JPanel 有setPreferredSize。

我遇到的问题是,当我减小 window 大小时,我声明了按钮的南面板被隐藏了。即使 window 减少了,我仍然希望按钮(南面板)对用户可见。

JPanel mainPanel = new JPanel(new BorderLayout());

//North panel
JPanel northPanel = new JPanel();
northPanel.setLayout(new GridBagLayout());
northPanel.setPreferredSize(new Dimension(600, 350));
northPanel.add(example..)
......

//South panel
JPanel southPanel = new JPanel(new BorderLayout()););
JButton button1 = new JButton();
JButton button2 = new JButton();
southPanel.add(button1, BorderLayout.WEST);
southPanel.add(button2, BorderLayout.EAST);

//Add north and south panels to main panel
this.mainPanel.add(northPanel, BorderLayout.NORTH); 
this.mainPanel.add(southPanel, BorderLayout.SOUTH); //want these buttons 
//to be visible even when window size is reduced. 
//but now the buttons gets hidden behind the north panel.

无论 window 大小如何,如何确保南面板始终可见?

希望我的解释有道理,谢谢!

The issue I have is when I reduce the window size, South Panel where I have buttons declared gets hidden.

面板上的组件是根据其 ZOrder 绘制的。当组件被添加到面板时,它被赋予更高的 ZOrder。 Swing 实际上首先绘制具有最高 ZOrder 的组件。

所以不要这样做:

panel.setLayout( new BorderLayout() );
panel.add( new JButton("NORTH"), BorderLayout.NORTH );
panel.add( new JButton("SOUTH"), BorderLayout.SOUTH );

你可以这样做:

panel.setLayout( new BorderLayout() );
panel.add( new JButton("SOUTH"), BorderLayout.SOUTH );
panel.add( new JButton("NORTH"), BorderLayout.NORTH );

现在 "South" 按钮将绘制在 "North" 按钮之上。当然,这只有在南组件不透明的情况下才有效。