title: Java Swing - MIDI Piano First Steps
body: <p>I'm only in my first stages of creating this project but I'm already lost. In my class we mainly focus on AWT, but for this project I was told to learn swing.</p>
  
  <p>Right now I am experimenting with Layouts to position the keys in the right positions as a normal keyboard would. I just really need help getting my layout on point. If someone could explain and provide code that would be fantastic. Thanks</p>
  
  <p>Code:</p>
  
  <pre><code>import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  import java.io.File;
  import java.util.ArrayList;
  import javax.sound.sampled.AudioInputStream;
  import javax.sound.sampled.AudioSystem;
  import javax.sound.sampled.Clip;
  
  public class main extends JFrame implements ActionListener
  {
  private JButton a1,a1s,b1,c1,c1s,d1,d1s,e1,f1,f1s,g1,g1s;
  private JPanel panel;
  private int x_pos = 50;
  String title = "INTERACTIVE PIANO";    
  
  
  private void createGUI()
  {
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    Container window = getContentPane();
    window.setLayout(new FlowLayout() );
  
    /*  panel = new JPanel();   
    panel.setPreferredSize(new Dimension(800, 700));
    panel.setBackground(Color.white);
    panel.setLayout(null);
    panel.setBackground(Color.gray);
    window.add(panel); */
    addKeys(); 
    }
  
    public void addKeys()
    {
      a1 = new JButton("A"); 
      {
        a1.setBackground(Color.white);
        a1.setPreferredSize(new Dimension(50, 150));
        a1.setLayout(null);
        a1.addActionListener(this);
        add(a1);
  
      } 
  
  
      a1s = new JButton("A#");
      {
        a1s.setBackground(Color.black);
        a1s.setPreferredSize(new Dimension(50, 100));
        a1s.setLayout(null);
        a1s.addActionListener(this);
        add(a1s);
      } 
  
  
      b1 = new JButton("B"); 
      {
        b1.setBackground(Color.white);
        b1.setPreferredSize(new Dimension(50, 150));
        b1.setLayout(null);
        b1.addActionListener(this);
        add(b1);
      }  
  
      c1 = new JButton("C"); 
      {
        c1.setBackground(Color.white);
        c1.setPreferredSize(new Dimension(50, 150));
        c1.setLayout(null);
        c1.addActionListener(this);
        add(c1);
      }  
  
      c1s = new JButton("C#");
      {
        c1s.setBackground(Color.black);
        c1s.setPreferredSize(new Dimension(50, 100));
        c1s.setLayout(null);
        c1s.addActionListener(this);
        add(c1s);
      } 
  
      d1 = new JButton("D"); 
      {
        d1.setBackground(Color.white);
        d1.setPreferredSize(new Dimension(50, 150));
        d1.setLayout(null);
        d1.addActionListener(this);
        add(d1);
      }  
  
      d1s = new JButton("D#");
      {
        d1s.setBackground(Color.black);
        d1s.setPreferredSize(new Dimension(50, 100));
        d1s.setLayout(null);
        d1s.addActionListener(this);
        add(d1s);
      } 
  
      e1 = new JButton("E"); 
      {
        e1.setBackground(Color.white);
        e1.setPreferredSize(new Dimension(50, 150));
        e1.setLayout(null);
        e1.addActionListener(this);
        add(e1);
      } 
  
      f1 = new JButton("F"); 
      {
        f1.setBackground(Color.white);
        f1.setPreferredSize(new Dimension(50, 150));
        f1.setLayout(null);
        f1.addActionListener(this);
        add(f1);
      }  
  
      f1s = new JButton("F#");
       {
        f1s.setBackground(Color.black);
        f1s.setPreferredSize(new Dimension(50, 100));
        f1s.setLayout(null);
        f1s.addActionListener(this);
        add(f1s);
      }  
  
      g1 = new JButton("G"); 
      {
        g1.setBackground(Color.white);
        g1.setPreferredSize(new Dimension(50, 150));
        g1.setLayout(null);
        g1.addActionListener(this);
        add(g1);
      }  
  
      g1s = new JButton("G#");
      {
        g1s.setBackground(Color.black);
        g1s.setPreferredSize(new Dimension(50, 100));
        g1s.setLayout(null);
        g1s.addActionListener(this);
        add(g1s);
      }  
  
   }
    public void actionPerformed(ActionEvent event)
    {
     //  Graphics paper = panel.getGraphics(); 
     if (event.getSource() == a1)
     {
  
     }
  
  
    } 
   public static void main(String[] args)
   {
    main frame = new main();
    frame.setTitle("Interactive Piano v1.0");
    frame.setSize(800, 700);
    frame.setMinimumSize(new Dimension(800, 700));
    frame.createGUI();
    frame.setVisible(true);
    frame.setLayout(null);
   }
  }
  </code></pre>