title: Java can a GridBagLayout be used to organize a JPanel used in a CardLayout
body: <p>I'm new to layouts and what I'm using now is a CardLayout with two different cards that currently have a background image painted on and a button.  As seen below, the button is at the top of the screen and I'd like to position it somewhere closer to the bottom.  My understanding is that a GridBagLayout would be the best way to accomplish that.  So my first question is, is that true?  And also, is it possible to do so if the JPanel I want to put the GridBagLayout on is a card in a CardLayout.  I'd like to put many other objects into the GridBagLayout (if that's the best way to do it and if it's possible) as I get further into the project, so any advice on where the right direction to go would be greatly appreciated.</p>
  
  <p><img src="https://i.imgur.com/cwiO2.jpg" alt=""></p>
  
  <pre><code>import java.awt.*;
  import java.awt.event.*;
  import java.io.IOException;
  import javax.imageio.ImageIO;
  import javax.swing.*;
  
  public class Cards implements ActionListener {    
  private JPanel cards;
  private JButton button1;
  private JButton button2;
  private Image backgroundImage;
  
  public void addComponentToPane(Container pane) throws IOException {
      try {           
          backgroundImage = ImageIO.read(getClass().getResource("resources/background.jpg"));         
      } catch (IOException ex) {
          ex.printStackTrace(System.err);
      }
  
      //create cards
      JPanel card1 = new JPanel()
      {
          @Override
          public void paintComponent(Graphics g)
          {
              super.paintComponent(g);
              g.drawImage(backgroundImage, 0, 0, null);
          }
  
          @Override
          public Dimension getPreferredSize() {
              return new Dimension(
                      backgroundImage.getWidth(null),
                      backgroundImage.getHeight(null));
          }
      };
      JPanel card2 = new JPanel()
      {
          @Override
          public void paintComponent(Graphics g)
          {
              super.paintComponent(g);
              g.drawImage(backgroundImage, 0, 0, null);
          }
  
          @Override
          public Dimension getPreferredSize() {
              return new Dimension(
                      backgroundImage.getWidth(null),
                      backgroundImage.getHeight(null));
          }
      };
      //create buttons
      button1 = new JButton("Button 1");
      button2 = new JButton("Button 2");
      button1.addActionListener(this);
      button2.addActionListener(this);
      //add buttons to cards
      card1.add(button1);        
      card2.add(button2);
      //create panel that contains cards
      cards = new JPanel(new CardLayout());
      cards.add(card1, "Card 1");
      cards.add(card2, "Card 2");
      pane.add(cards, BorderLayout.SOUTH);        
  }
  
  public void itemStateChanged(ItemEvent evt) {
      CardLayout cl = (CardLayout)(cards.getLayout());
      cl.show(cards, (String)evt.getItem());
  }
  
  public static void createAndShowGUI() throws IOException  {
      //create and setup window
      JFrame frame = new JFrame("Frame"); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
      //create and setup content pane
      Cards main = new Cards();
      main.addComponentToPane(frame.getContentPane());        
      //display window
      frame.pack();       
      //frame.setResizable(false);
      frame.setVisible(true);
  }
  
  public void actionPerformed(ActionEvent ae) {
      if (ae.getSource() == button1) {
          CardLayout cl = (CardLayout) (cards.getLayout());
          cl.show(cards, "Card 2");     
      } else if (ae.getSource() == button2) {
          CardLayout cl = (CardLayout) (cards.getLayout());
          cl.show(cards, "Card 1");
      }        
  }            
  
  public static void main(String[] args) {
      //set look and feel
      try {
          UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
      } catch (UnsupportedLookAndFeelException ex) {
          ex.printStackTrace();
      } catch (IllegalAccessException ex) {
          ex.printStackTrace();
      } catch (InstantiationException ex) {
          ex.printStackTrace();
      } catch (ClassNotFoundException ex) {
          ex.printStackTrace();
      }                
  
      //schedule job for the event dispatch thread creating and showing GUI        
      javax.swing.SwingUtilities.invokeLater(new Runnable() {
          public void run() {                   
                      try {
                          createAndShowGUI();
                      } catch (IOException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }                
          }
      });     
  }   
  }
  </code></pre>