title: Using JButtons, JLabels, and JTextFields within a JPanel over a painted background
body: <p>I'm pretty new to Java, but have been using <code>JPanels</code>, <code>JButtons</code>, <code>JLabels</code> and <code>JTextFields</code> successfully in other parts of my program, but I'm running into trouble when trying to have a <code>JPanel</code> with a couple <code>JButtons</code>, <code>JLabels</code>, and <code>JTextFields</code> inside of it overtop of a painted background. </p>
  
  <p>Here is all of the relevant code for this portion of the program.  Currently, when the program runs the only thing that will display is the <code>p1_fireButton</code>, <code>player1PowerField</code>, and <code>player1AngleField</code> overtop of the background (Even though I have the components created for player 2, I purposely commented out adding the <code>p2_Panel</code> so that I could concentrate on <code>p1_Panel</code>.  </p>
  
  <p>The weird thing is that those <code>JComponents</code>  will only display if I hit the TAB key after the program is running for whatever reason, which I'm also hoping someone could help me fix.  My goal will be to have the <code>p1_panel</code> occupy the left orange box and the <code>p2_panel</code> occupy the right orange box. Help would be greatly appreciated!</p>
  
  <p><img src="https://i.stack.imgur.com/pVHOs.png" alt="enter image description here"></p>
  
  <pre><code>public class GameFrame extends JFrame
  { //start class GameFrame
  
      ImageIcon background = new ImageIcon("background.jpg");
      ImageIcon terrain1 = new ImageIcon("terrain1.png");
      //ImageIcon tank_red = new ImageIcon("tank_red.png");
      //ImageIcon tank_red = new ImageIcon(player1Tank);
  
      private int x_rectangle = 50;
      private int y_rectangle = 50;
  
      private JButton p1_fireButton;
      private JButton p2_fireButton;
      private JPanel p1_Panel;
      private JPanel p2_Panel;
  
      private JLabel player1PowerLabel;
      private static JTextField player1PowerField;
      private JLabel player1AngleLabel;
      private static JTextField player1AngleField;
  
      private JLabel player2PowerLabel;
      private static JTextField player2PowerField;
      private JLabel player2AngleLabel;
      private static JTextField player2AngleField;
  
      String player1Name;
      String player2Name;
      final Timer gameTimer = new Timer(8, new timer());
  
      Projectile projectile = new Projectile(200, 300);
  
      public GameFrame(String title)
      { //start GameFrame constructor
          super(title);
          Dimension size = getPreferredSize();
          size.width = 1000;
          setPreferredSize(size);
          setResizable(false);
          setLayout(null);
          Color trans = new Color(0, 0, 0, 0);
  
          //player1 panel
          p1_Panel = new JPanel();
          p1_Panel.setLayout(null);
          p1_Panel.setBounds(0, 0, 500, 300);
          p1_Panel.setBackground(trans);
  
          p2_Panel = new JPanel();
          p2_Panel.setLayout(null);
          p2_Panel.setBounds(500, 0, 500, 300);
          p2_Panel.setBackground(trans);
  
          //player2 panel
          /*p2_fireButtonPanel = new JPanel();
          p2_fireButtonPanel.setBounds(400, 85, 100, 100);
          p2_fireButtonPanel.setBackground(trans);*/
  
          //player1 angle/power fields
          player1PowerLabel = new JLabel("Power");
          player1PowerLabel.setLayout(null);
          player1PowerLabel.setBounds(400, 20, 50, 50);
  
          player1PowerField = new JTextField(3);
          player1PowerField.setLayout(null);
          player1PowerField.setBounds(400, 10, 50, 25);
  
          player1AngleLabel = new JLabel("Angle");
          player1AngleLabel.setLayout(null);
          player1AngleLabel.setBounds(30, 10, 50, 50);
  
          player1AngleField = new JTextField(3);
          player1AngleField.setLayout(null);
          player1AngleField.setBounds(300, 10, 50, 25);
  
  
          //player2 angle/power fields
          player2PowerLabel = new JLabel("Power");
          player2PowerLabel.setLayout(null);
          player2PowerLabel.setBounds(0, 0, 10, 10);
  
          player2PowerField = new JTextField(3);
          player2PowerField.setLayout(null);
          player2PowerField.setBounds(10, 10, 10, 10);
  
          player2AngleLabel = new JLabel("Angle");
          player2AngleLabel.setLayout(null);
          player2AngleLabel.setBounds(30, 10, 10, 10);
  
          player2AngleField = new JTextField(3);
          player2AngleField.setLayout(null);
          player2AngleField.setBounds(60, 10, 10, 10);
  
  
          //player1 fire button
          p1_fireButton = new JButton("Fire!");
          p1_fireButton.setLayout(null);
          p1_fireButton.setBounds(430, 70, 50, 50);
          ActionListener fireListener = new fireButtonListener();
          p1_fireButton.addActionListener(fireListener);
  
          //player2 fire button
          p2_fireButton = new JButton("Fire AGAIN!");
          p2_fireButton.setLayout(null);
          p2_fireButton.setBounds(530, 70, 50, 50);
  
  
          //add components to player1 panel
          p1_Panel.add(p1_fireButton);
          p1_Panel.add(player1PowerLabel);
          p1_Panel.add(player1PowerField);
          p1_Panel.add(player1AngleLabel);
          p1_Panel.add(player1AngleField);
  
          //add components to player2 panel
          p2_Panel.add(p2_fireButton);
          p2_Panel.add(player2PowerLabel);
          p2_Panel.add(player2PowerField);
          p2_Panel.add(player2AngleLabel);
          p2_Panel.add(player2AngleField);
  
          //add components to GameFrame
          add(p1_Panel);
          //add(p2_Panel);
  
          projectile.fireProjectile(60, -60 * Math.PI / 180.0);
  
      } //end GameFrame constructor
  
      public void paint(Graphics g)
      {
          Graphics2D g2 = (Graphics2D) g;
  
          Image bg = background.getImage();
          Image t1 = terrain1.getImage();
          Image p1tank = StartPanel.getPlayer1Tank();
          Image p2tank = StartPanel.getPlayer2Tank();
          //Image tank1 = tank_red.getImage();
  
          g2.drawImage(bg,  0, 0, 1000, 800, this);
          g2.drawImage(t1,  0, 420, 1000, 380, this);
  
  
          g2.drawImage(p1tank, 50, 300, 66, 50, null);
          g2.drawImage(p2tank, 500, 300, 66, 50, null);
  
          player1Name = new String(StartPanel.getPlayer1Name());
          player2Name = new String(StartPanel.getPlayer2Name());
  
          g.drawString(player1Name, 50, 50);
          g.drawString(player2Name, 525, 50);
  
          g2.setColor(Color.green);
          g2.fillOval((int)projectile.getXPosition(), (int)projectile.getYPosition(), 15, 15);
  
  
      }
  
      public class timer implements ActionListener
      {
  
          public void actionPerformed(ActionEvent e)
          {    
              //TanksGUI.gameFrame.moveRectangle(1, 1);
              projectile.advanceProjectile(0.05);
  
              if (projectile.getYPosition() &gt; 400)
              {
                  gameTimer.stop();
              }
              repaint();
  
          }
  
      }
      public class fireButtonListener implements ActionListener
      {
          public void actionPerformed(ActionEvent e)
          {
  
              gameTimer.start();
          }
      }
  
  
  } //end class GameFrame
  </code></pre>