Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think LayeredPane is your best bet here. You would need a third panel though to contain A and B. This third panel would be the layeredPane and then panel A and B could still have a nice LayoutManagers. All you would have to do is center B over A and there is quite a lot of examples in the Swing trail on how to do this. <a href="http://java.sun.com/docs/books/tutorial/uiswing/layout/none.html" rel="noreferrer">Tutorial for positioning without a LayoutManager</a>.</p> <pre><code>public class Main { private JFrame frame = new JFrame(); private JLayeredPane lpane = new JLayeredPane(); private JPanel panelBlue = new JPanel(); private JPanel panelGreen = new JPanel(); public Main() { frame.setPreferredSize(new Dimension(600, 400)); frame.setLayout(new BorderLayout()); frame.add(lpane, BorderLayout.CENTER); lpane.setBounds(0, 0, 600, 400); panelBlue.setBackground(Color.BLUE); panelBlue.setBounds(0, 0, 600, 400); panelBlue.setOpaque(true); panelGreen.setBackground(Color.GREEN); panelGreen.setBounds(200, 100, 100, 100); panelGreen.setOpaque(true); lpane.add(panelBlue, new Integer(0), 0); lpane.add(panelGreen, new Integer(1), 0); frame.pack(); frame.setVisible(true); } /** * @param args the command line arguments */ public static void main(String[] args) { new Main(); } } </code></pre> <p>You use setBounds to position the panels inside the layered pane and also to set their sizes.</p> <p><strong>Edit to reflect changes to original post</strong> You will need to add component listeners that detect when the parent container is being resized and then dynamically change the bounds of panel A and B.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload