title: How to absolutely position a JPanel over another JPanel with setLayout(null) and a background image?
body: <p>I'm working with JFrames in Java, specifically with absolutely positioned elements that need to overlap. I understand that to overlay components, one should make a JPanel (with <code>setOpacity(false);</code>), and position it with either <code>setBounds(x,y,x2,y2);</code> or <code>setPosition(x,y)</code> &amp; <code>setSize(x,y)</code>. Unfortunately the panels act like CSS's <code>inline-divs</code>; they take up only the needed amount of room on their line, and do not stack.</p>
  
  <p>This is the code I have so far, but it doesn't seem to act like I'd imagine it would:</p>
  
  <pre><code>class Login extends JFrame {
      private JPanel         backgroundpanel;
      private JPanel         panel;
      private JPanel         panel2;
      private JTextField     usernameBox;
      private JPasswordField passwordBox;
      private JButton        button;
      private int            height = 319;
      private int            width  = 452;
      private ImageIcon      ii     = new ImageIcon("special-window-BG.png");
      private JLabel         image;
  
      public Login() {
          setLayout(null);
          setTitle("Login");
          setSize(width,height);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setLayout(null);
  
          buildPanel();
  
          add(backgroundpanel);
          setVisible(true);
      }
  
      private void buildPanel() {
          usernameBox = new JTextField(20);
          passwordBox = new JPasswordField(20);
          button = new JButton("Login");
          image = new JLabel(ii);
  
          backgroundpanel = new JPanel();
          panel = new JPanel();
          panel2 = new JPanel();
  
          backgroundpanel.add(panel);
          backgroundpanel.add(panel2);
          backgroundpanel.add(image);
  
          panel.setBackground(Color.red);
          panel.setBounds(0, 0, 10, 10);
          panel.setOpaque(false);
  
          panel2.setBackground(Color.blue);
          panel2.setBounds(0, 0, 10, 10);
          panel2.setOpaque(false);
  
          panel.add(passwordBox);
          panel2.add(button);
  
          backgroundpanel.setOpaque(false);
          backgroundpanel.isOptimizedDrawingEnabled();
          backgroundpanel.setBounds(0, 0, width, height);
  </code></pre>
  
  <p>...cot'd, however unnecessary.</p>
  
  <p>So basically, I'd like to know how to absolutely position JPanels (or JComponents, if that's simpler) over a JPanel with a background-image.</p>
  
  <p>Thanks for taking a look at this question, I've spent far too much time on this method; the commented-out code extends nearly 500 lines passed what I posted, so I have nowhere else to turn to. The image below shows a crude illustration of what I'm trying to accomplish, I'm not sure if I'actually come close to getting it yet, because sometimes the JComponents seem to disappear as if they're behind the background image, however I'd like to find the simple solution that's most likely right in front of my eyes!</p>
  
  <p><a href="http://i.stack.imgur.com/revz8.jpg" rel="nofollow">http://i.stack.imgur.com/revz8.jpg</a></p>