title: Positioning of Swing Components Java - JPanel, JButton, JFrame
body: <p>I'm currently studying Java GUI implementations and for my next assignment we are to create a program to simulate the control of a house. We have been given pretty much free reign to build it however we like (as long as its in Java). I've drawn up this mockup: <a href="https://i.imgur.com/9RtWL7b.jpg" rel="nofollow noreferrer">http://i.imgur.com/9RtWL7b.jpg</a> (shown below) and thats what I'm looking to produce. </p>
  
  <p>Program Mockup:
  <img src="https://i.stack.imgur.com/LlIb2.jpg" alt="Program Mockup"></p>
  
  <p>Below is the code I have at the moment and it produces this <a href="https://i.imgur.com/XZLiwWx.jpg" rel="nofollow noreferrer">http://i.imgur.com/XZLiwWx.jpg</a> (below)</p>
  
  <p>Example Code Below:
  <img src="https://i.stack.imgur.com/mBPfF.jpg" alt="Program Example"></p>
  
  <p>My question is; how can I get the three buttons to be in the to left of the screen? Am I going about this the wrong way? I feel like the code I have below is quite clunky and un-organised, should I be taking a different approach?</p>
  
  <p>Let me know if any more information is needed.</p>
  
  <pre><code>import java.awt.*;
  import java.awt.Dimension;
  import java.awt.Graphics;
  import java.awt.Image;
  
  import javax.swing.ImageIcon;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  import javax.swing.JButton;
  
  public class ImageTest {
  
      public static void main(String[] args) {
          ImagePanel panel            = new ImagePanel("program/assets/main_bg.jpg");
  
          TopTabButton buttonHome     = new TopTabButton("home");
          TopTabButton buttonSettings = new TopTabButton("settings");
          TopTabButton buttonHelp     = new TopTabButton("help");
  
          panel.add(buttonHome);
          panel.add(buttonSettings);
          panel.add(buttonHelp);
  
  
          panel.setPreferredSize(new Dimension(1000, 760));
  
          JFrame frame = new JFrame();
          frame.setBackground(new Color(53, 56, 64));
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
          frame.getContentPane().add(panel);
  
          frame.pack();
  
          frame.setLocationRelativeTo(null);
          frame.setResizable(false);
          frame.setVisible(true);
      }
  
  }
  
  class TopTabButton extends JButton {
  
      public TopTabButton(String buttonCode) {
          setIcon(new ImageIcon("program/assets/top_tab_button_bg_" + buttonCode + "_inactive.png"));
          setRolloverIcon(new ImageIcon("program/assets/top_tab_button_bg_" + buttonCode + "_active.png"));
          setBorderPainted(false);
          setFocusPainted(false);
          setContentAreaFilled(true);
          setSize(new Dimension(126, 75));
       }
  
  }
  
  class ImagePanel extends JPanel {
  
      private Image img;
  
      public ImagePanel(String img) {
          this(Toolkit.getDefaultToolkit().createImage(img));
      }
  
      public ImagePanel(Image img) {
          this.img = img;
          Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
          setPreferredSize(size);
          setMinimumSize(size);
          setMaximumSize(size);
          setSize(size);
          setLayout(new GridBagLayout());
      }
  
      public void paintComponent(Graphics g) {
          g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
      }
  }
  
  class ButtonPanelTest extends JPanel {
  
      private Image img;
  
      public ButtonPanelTest(String name) {
          Button Button1 = new Button(name);
          Dimension size = new Dimension(100, 50);
          setSize(size);
          setLayout(new GridBagLayout());
      }
  
  }
  </code></pre>