Results 1 to 9 of 9
  1. #1
    sarah jain is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default Setting sizes to JButtons in grid layout

    Hello Everyone,

    I am trying to design a piano using JButtons and add sounds to the buttons. Here is my code. I did not add the the part of the code which gives sound to the buttons as I have no problem with it. My problem with this part of the code is I cannot set the size for JButtons. I used setPreferedSize for setting the size, but it did not work.

    Java Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    
    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.Clip;
    import javax.sound.sampled.LineUnavailableException;
    import javax.sound.sampled.UnsupportedAudioFileException;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    import java.awt.GridLayout;
    import java.awt.Dimension;
    
    
    public class sample extends Applet implements ActionListener{
      JButton a, b, c, d, e, f, g, h, i, j, k, l;
      Clip clip;
      AudioInputStream song;
    
      public void init(){
    	
    	  setLayout(new GridLayout(1,9)); 
    	final JFrame frame = new JFrame();
    	frame.setPreferredSize(new Dimension(600, 450));
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.setBackground(Color.green.darker());
    
        a = new JButton("3-1");
        a.setBackground(Color.white);
       
        a.setPreferredSize(new Dimension(40, 240));
        
        add(a);
        a.addActionListener(this);
        b = new JButton("3-2");
        b.setBackground(Color.black);
        a.setPreferredSize(new Dimension(20, 160));
        
        add(b);
        b.addActionListener(this);
        
        c = new JButton("3-3");
        a.setBackground(Color.white);
       
        c.setPreferredSize(new Dimension(40, 240));
        
        add(c);
        c.addActionListener(this);
        
        d = new JButton("3-4");
        d.setBackground(Color.black);
        d.setPreferredSize(new Dimension(20, 160));
        
        add(d);
        d.addActionListener(this);
        
        e = new JButton("3-5");
        e.setBackground(Color.white);
       
        e.setPreferredSize(new Dimension(40, 240));
        
        add(e);
        e.addActionListener(this);
        f = new JButton("3-6");
        f.setBackground(Color.white);
        f.setPreferredSize(new Dimension(40, 240));
        
        add(f);
        f.addActionListener(this);
        
        g = new JButton("3-7");
        g.setBackground(Color.black);
       
        g.setPreferredSize(new Dimension(20, 160));
        
        add(g);
        g.addActionListener(this);
        h = new JButton("3-8");
        h.setBackground(Color.white);
        h.setPreferredSize(new Dimension(40, 140));
        
        add(h);
        h.addActionListener(this);
        
        i = new JButton("3-9");
        i.setBackground(Color.black);
       
        i.setPreferredSize(new Dimension(20, 160));
        
        add(i);
        i.addActionListener(this);
        j = new JButton("3-10");
        j.setBackground(Color.white);
        j.setPreferredSize(new Dimension(40, 240));
        
        add(j);
        j.addActionListener(this);
        
        k = new JButton("3-11");
        k.setBackground(Color.black);
        k.setPreferredSize(new Dimension(20, 160));
        
        add(k);
        k.addActionListener(this);
        
        l = new JButton("3-12");
        l.setBackground(Color.white);
        l.setPreferredSize(new Dimension(40, 240));
        
        add(l);
        l.addActionListener(this);
      
      }

    I want my out put to be as the image displayed below. If this image cannot be designed using gridlayout, please let me know what other layouts I can use.

    Thank you,
    sarah
    Attached Thumbnails Attached Thumbnails Setting sizes to JButtons in grid layout-free-piano-chords-1.jpg  
    Last edited by Fubarable; 02-20-2011 at 01:09 AM. Reason: Moderator Edit: Code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    An interesting question. One possible solution is to use a JLayeredPane that holds two JPanels, one for the white keys and one for the black keys, both JPanels use GridLayout(1, 0), have the white key JPanel the full hight of the layered pane, have the black key JPanel have the height of the layered pane, and give the black key JPanel black keys or invisible JLabels. It would need a ComponentListener to resize the black and white panels if the JLayeredPane changed size. Something like so:

    Java Code:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import java.awt.event.ComponentAdapter;
    import java.awt.event.ComponentEvent;
    
    import javax.swing.*;
    
    public class PianoKeys  {
       private static final Dimension PREF_SIZE = new Dimension(700, 400);
       private static final int[] BLACK_KEY_LOCATIONS = {1, 3, 7, 9, 11};
       private static final int BLACK_KEY_GRID_ROW_COUNT = 14;
       private JLayeredPane mainPanel = new JLayeredPane();
       private JButton[] whiteKeys = new JButton[7];
       private JButton[] blackKeys = new JButton[5];
       private JPanel whiteKeyPanel = new JPanel(new GridLayout(1, 0,1, 1));
       private JPanel blackKeyPanel = new JPanel(new GridLayout(1, 0, 1, 1));
    
       public PianoKeys() {
          mainPanel.setPreferredSize(PREF_SIZE);
          int blackShift = PREF_SIZE.width / (BLACK_KEY_GRID_ROW_COUNT * 2);
          blackKeyPanel.setLocation(blackShift, 0);
          whiteKeyPanel.setLocation(0, 0);
          
          whiteKeyPanel.setSize(PREF_SIZE);
          blackKeyPanel.setSize(new Dimension(PREF_SIZE.width, PREF_SIZE.height/2));
          
          mainPanel.addComponentListener(new MyComponentAdapter());
          blackKeyPanel.setOpaque(false);
          whiteKeyPanel.setBackground(Color.black);
          
          for (int i = 0; i < whiteKeys.length; i++) {
             JButton whiteKeyBtn = new JButton();
             whiteKeyBtn.setBackground(Color.white);
             whiteKeyPanel.add(whiteKeyBtn);
          }
          
          int blackKeyCount = 0;
          for (int i = 0; i < BLACK_KEY_GRID_ROW_COUNT; i++) {
             boolean noBlackKey = true;
             for (int j = 0; j < BLACK_KEY_LOCATIONS.length; j++) {
                if (i == BLACK_KEY_LOCATIONS[j]) {
                   blackKeys[blackKeyCount] = new JButton();
                   blackKeys[blackKeyCount].setBackground(Color.black);
                   blackKeyPanel.add(blackKeys[blackKeyCount]);
                   noBlackKey = false;
                   blackKeyCount++;
                }
             }
             if (noBlackKey) {
                blackKeyPanel.add(new JLabel());
             }
          }
          
          mainPanel.add(whiteKeyPanel, JLayeredPane.DEFAULT_LAYER);
          mainPanel.add(blackKeyPanel, new Integer(10));
       }
    
       public JComponent getMainPanel() {
          return mainPanel;
       }
       
       private class MyComponentAdapter extends ComponentAdapter {
          @Override
          public void componentResized(ComponentEvent e) {
             Dimension size = mainPanel.getSize();
             int blackShift = size.width / (BLACK_KEY_GRID_ROW_COUNT * 2);
             blackKeyPanel.setLocation(blackShift, 0);
    
             whiteKeyPanel.setSize(size);
             blackKeyPanel.setSize(new Dimension(size.width, size.height/2));
             
             mainPanel.revalidate();
             mainPanel.repaint();
          }
       }
    
       private static void createAndShowUI() {
          JFrame frame = new JFrame("PianoKeys");
          frame.getContentPane().add(new PianoKeys().getMainPanel());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    An interesting question. One possible solution is to use a JLayeredPane that holds two JPanels, one for the white keys and one for the black keys, both JPanels use GridLayout(1, 0), have the white key JPanel the full hight of the layered pane, have the black key JPanel have the height of the layered pane, and give the black key JPanel black keys or invisible JLabels. It would need a ComponentListener to resize the black and white panels if the JLayeredPane changed size. Something like so:

    [ ... ]
    Cute solution; one extremely minor and extremely off-topic nitpick: your black keys are half the height of those white keys (the visible part); piano players don't like that, i.e. it feels like they have to stretch their fingers too much to press those asccidentals; they are happy with a ratio 1: 27/48 though. So if you change the following two lines in your code you can be sure that you'll never get hit by a defenestrated piano ;-)

    Java Code:
          blackKeyPanel.setSize(new Dimension(PREF_SIZE.width, 28*PREF_SIZE.height/47));
     
            blackKeyPanel.setSize(new Dimension(size.width, 28*size.height/47));
    kind regards,

    Jos (<--- pathetic nitpicker)
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by JosAH View Post
    Jos (<--- pathetic nitpicker)
    Thank you for your kind constructive criticisms. Now do let me show you the lovely view from the window over there. Don't mind the angry Pascal mobs below and their pointy sticks.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    Thank you for your kind constructive criticisms. Now do let me show you the lovely view from the window over there. Don't mind the angry Pascal mobs below and their pointy sticks.
    Throw one of your half baked pianos on their teethless heads! Go get them! ;-)

    kindest regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    sarah jain is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default

    Thank you so much for the solution. For assigning sound to the jbuttons I gave labels to the buttons like, "3-1, 3-2..." and used getlabel() method. Since in this code the buttons are not labeled, I cannot use getlabel(). Is there any other way to assign sound to the buttons. This is my code for assigning sound(only to the first button)

    public void actionPerformed(ActionEvent ae){

    JButton source = (JButton)ae.getSource();
    if (source.getLabel() == "3-1"){
    File file = new File("C:\\Users\\Snehitha\\Desktop\\1.AU");
    try {
    song = AudioSystem.getAudioInputStream(file);
    } catch (UnsupportedAudioFileException e) {

    e.printStackTrace();
    } catch (IOException e) {

    e.printStackTrace();
    }
    try {
    clip=AudioSystem.getClip();
    } catch (LineUnavailableException e) {

    e.printStackTrace();
    }
    try {
    clip.open(song);
    } catch (LineUnavailableException e) {

    e.printStackTrace();
    } catch (IOException e) {

    e.printStackTrace();
    }
    clip.start();
    }
    }
    }

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The buttons can be in an array (as I show) and you can get the button reference from the ActionEvent's getSource() method, then iterate through the arrays to find the button. Another possible way is to set each button's actionCommand property and obtain that, again from the ActionEvent object passed into your actionPerformed method.

  8. #8
    sarah jain is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default

    Hello,
    I made few modifications to ur code, because I didn't know how to assign sound to the buttons without labeling them. But on executing the code, the black buttons are not positioned at 1, 3, 7,9, 11 location. Please let me know where I made a mistake

    import java.awt.*;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ComponentAdapter;
    import java.awt.event.ComponentEvent;
    import java.io.File;
    import java.io.IOException;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.LineUnavailableException;
    import javax.sound.sampled.UnsupportedAudioFileException;
    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.Clip;
    import javax.swing.*;

    public class help implements ActionListener {
    private static final Dimension PREF_SIZE = new Dimension(700, 400);
    private static final int[] BLACK_KEY_LOCATIONS = {1, 3, 7, 9, 11};
    private static final int BLACK_KEY_GRID_ROW_COUNT = 14;
    private JLayeredPane mainPanel = new JLayeredPane();
    private JPanel whiteKeyPanel = new JPanel(new GridLayout(1, 0,1, 1));
    private JPanel blackKeyPanel = new JPanel(new GridLayout(1, 0, 1, 1));
    JButton a,b,c,d,e,f,g,h,i,j,k,l;
    int x=1, y=3, z=7, o=9, p=11;
    Clip clip;
    AudioInputStream song;
    public help() {
    mainPanel.setPreferredSize(PREF_SIZE);
    int blackShift = PREF_SIZE.width / (BLACK_KEY_GRID_ROW_COUNT * 2);
    blackKeyPanel.setLocation(blackShift, 0);
    whiteKeyPanel.setLocation(0, 0);

    whiteKeyPanel.setSize(PREF_SIZE);
    blackKeyPanel.setSize(new Dimension(PREF_SIZE.width, PREF_SIZE.height/2));

    mainPanel.addComponentListener(new MyComponentAdapter());
    blackKeyPanel.setOpaque(false);
    whiteKeyPanel.setBackground(Color.black);


    //Adding black and white buttons
    boolean noBlackKey = true;

    a = new JButton("3-1");
    a.setBackground(Color.white);
    whiteKeyPanel.add(a);
    a.addActionListener(this);
    if(x == BLACK_KEY_LOCATIONS[0] ){
    b = new JButton("3-2");
    b.setBackground(Color.black);
    blackKeyPanel.add(b);
    b.addActionListener(this);
    noBlackKey = false;
    }
    else if (noBlackKey) {
    blackKeyPanel.add(new JLabel());
    }
    c = new JButton("3-3");
    c.setBackground(Color.white);
    whiteKeyPanel.add(c);
    c.addActionListener(this);
    if(y== BLACK_KEY_LOCATIONS[1]){
    d = new JButton("3-4");
    d.setBackground(Color.black);
    blackKeyPanel.add(d);
    d.addActionListener(this);
    noBlackKey = false;
    }
    else if (noBlackKey) {
    blackKeyPanel.add(new JLabel());
    }
    e = new JButton("3-5");
    e.setBackground(Color.white);
    whiteKeyPanel.add(e);
    e.addActionListener(this);

    f = new JButton("3-6");
    f.setBackground(Color.white);
    whiteKeyPanel.add(f);
    f.addActionListener(this);

    if(z==BLACK_KEY_LOCATIONS[2]){
    g = new JButton("3-7");
    g.setBackground(Color.black);
    blackKeyPanel.add(g);
    g.addActionListener(this);
    noBlackKey = false;
    }
    else if (noBlackKey) {
    blackKeyPanel.add(new JLabel());
    }

    h = new JButton("3-8");
    h.setBackground(Color.white);
    whiteKeyPanel.add(h);
    h.addActionListener(this);

    if(o==BLACK_KEY_LOCATIONS[3]){
    i = new JButton("3-9");
    i.setBackground(Color.black);
    blackKeyPanel.add(i);
    i.addActionListener(this);
    noBlackKey = false;
    }
    else if (noBlackKey) {
    blackKeyPanel.add(new JLabel());
    }

    j = new JButton("3-10");
    j.setBackground(Color.white);
    whiteKeyPanel.add(j);
    j.addActionListener(this);

    if(p==BLACK_KEY_LOCATIONS[4]){
    k = new JButton("3-11");
    k.setBackground(Color.black);
    blackKeyPanel.add(k);
    k.addActionListener(this);
    noBlackKey = false;
    }
    else if (noBlackKey) {
    blackKeyPanel.add(new JLabel());
    }
    l = new JButton("3-12");
    l.setBackground(Color.white);
    whiteKeyPanel.add(l);
    l.addActionListener(this);

    mainPanel.add(whiteKeyPanel, JLayeredPane.DEFAULT_LAYER);
    mainPanel.add(blackKeyPanel, new Integer(10));
    }

    //Adding sound clip to the buttons
    public void actionPerformed(ActionEvent ae){

    JButton source = (JButton)ae.getSource();
    if (source.getLabel() == "3-1"){
    File file = new File("C:\\Users\\Snehitha\\Desktop\\1.AU");
    try {
    song = AudioSystem.getAudioInputStream(file);
    } catch (UnsupportedAudioFileException e) {

    e.printStackTrace();
    } catch (IOException e) {

    e.printStackTrace();
    }
    try {
    clip=AudioSystem.getClip();
    } catch (LineUnavailableException e) {

    e.printStackTrace();
    }
    try {
    clip.open(song);
    } catch (LineUnavailableException e) {

    e.printStackTrace();
    } catch (IOException e) {

    e.printStackTrace();
    }
    clip.start();
    }
    else if(source.getLabel() == "3-2"){
    //JButton sour = (JButton)ae.getSource();
    // if (sour.getLabel() == "3-2"){
    File file = new File("C:\\Users\\Snehitha\\Desktop\\2.AU");
    try {
    song = AudioSystem.getAudioInputStream(file);
    } catch (UnsupportedAudioFileException e) {

    e.printStackTrace();
    } catch (IOException e) {

    e.printStackTrace();
    }
    try {
    clip=AudioSystem.getClip();
    } catch (LineUnavailableException e) {

    e.printStackTrace();
    }
    try {
    clip.open(song);
    } catch (LineUnavailableException e) {

    e.printStackTrace();
    } catch (IOException e) {

    e.printStackTrace();
    }
    clip.start();
    }

    else if(source.getLabel() == "3-3"){
    // JButton soure = (JButton)ae.getSource();
    // if (sour.getLabel() == "3-2"){
    File file = new File("C:\\Users\\Snehitha\\Desktop\\3.AU");
    try {
    song = AudioSystem.getAudioInputStream(file);
    } catch (UnsupportedAudioFileException e) {

    e.printStackTrace();
    } catch (IOException e) {

    e.printStackTrace();
    }
    try {
    clip=AudioSystem.getClip();
    } catch (LineUnavailableException e) {

    e.printStackTrace();
    }
    try {
    clip.open(song);
    } catch (LineUnavailableException e) {

    e.printStackTrace();
    } catch (IOException e) {

    e.printStackTrace();
    }
    clip.start();
    }
    else if(source.getLabel() == "3-4"){
    File file = new File("C:\\Users\\Snehitha\\Desktop\\4.AU");
    try {
    song = AudioSystem.getAudioInputStream(file);
    } catch (UnsupportedAudioFileException e) {

    e.printStackTrace();
    } catch (IOException e) {

    e.printStackTrace();
    }
    try {
    clip=AudioSystem.getClip();
    } catch (LineUnavailableException e) {

    e.printStackTrace();
    }
    try {
    clip.open(song);
    } catch (LineUnavailableException e) {

    e.printStackTrace();
    } catch (IOException e) {

    e.printStackTrace();
    }
    clip.start();
    }
    else if(source.getLabel() == "3-5"){
    // JButton soure2 = (JButton)ae.getSource();
    // if (sour.getLabel() == "3-2"){
    File file = new File("C:\\Users\\Snehitha\\Desktop\\5.AU");
    try {
    song = AudioSystem.getAudioInputStream(file);
    } catch (UnsupportedAudioFileException e) {

    e.printStackTrace();
    } catch (IOException e) {

    e.printStackTrace();
    }
    try {
    clip=AudioSystem.getClip();
    } catch (LineUnavailableException e) {

    e.printStackTrace();
    }
    try {
    clip.open(song);
    } catch (LineUnavailableException e) {

    e.printStackTrace();
    } catch (IOException e) {

    e.printStackTrace();
    }
    clip.start();
    }
    else if(source.getLabel() == "3-6"){
    File file = new File("C:\\Users\\Snehitha\\Desktop\\6.AU");
    try {
    song = AudioSystem.getAudioInputStream(file);
    } catch (UnsupportedAudioFileException e) {

    e.printStackTrace();
    } catch (IOException e) {

    e.printStackTrace();
    }
    try {
    clip=AudioSystem.getClip();
    } catch (LineUnavailableException e) {

    e.printStackTrace();
    }
    try {
    clip.open(song);
    } catch (LineUnavailableException e) {

    e.printStackTrace();
    } catch (IOException e) {

    e.printStackTrace();
    }
    clip.start();
    }
    else if(source.getLabel() == "3-7"){
    //JButton soure = (JButton)ae.getSource();
    // if (sour.getLabel() == "3-2"){
    File file = new File("C:\\Users\\Snehitha\\Desktop\\7.AU");
    try {
    song = AudioSystem.getAudioInputStream(file);
    } catch (UnsupportedAudioFileException e) {

    e.printStackTrace();
    } catch (IOException e) {

    e.printStackTrace();
    }
    try {
    clip=AudioSystem.getClip();
    } catch (LineUnavailableException e) {

    e.printStackTrace();
    }
    try {
    clip.open(song);
    } catch (LineUnavailableException e) {

    e.printStackTrace();
    } catch (IOException e) {

    e.printStackTrace();
    }
    clip.start();
    }
    else if(source.getLabel() == "3-8"){
    // JButton soure = (JButton)ae.getSource();
    // if (sour.getLabel() == "3-2"){
    File file = new File("C:\\Users\\Snehitha\\Desktop\\8.AU");
    try {
    song = AudioSystem.getAudioInputStream(file);
    } catch (UnsupportedAudioFileException e) {

    e.printStackTrace();
    } catch (IOException e) {

    e.printStackTrace();
    }
    try {
    clip=AudioSystem.getClip();
    } catch (LineUnavailableException e) {

    e.printStackTrace();
    }
    try {
    clip.open(song);
    } catch (LineUnavailableException e) {

    e.printStackTrace();
    } catch (IOException e) {

    e.printStackTrace();
    }
    clip.start();
    }
    else if(source.getLabel() == "3-9"){
    File file = new File("C:\\Users\\Snehitha\\Desktop\\9.AU");
    try {
    song = AudioSystem.getAudioInputStream(file);
    } catch (UnsupportedAudioFileException e) {

    e.printStackTrace();
    } catch (IOException e) {

    e.printStackTrace();
    }
    try {
    clip=AudioSystem.getClip();
    } catch (LineUnavailableException e) {

    e.printStackTrace();
    }
    try {
    clip.open(song);
    } catch (LineUnavailableException e) {

    e.printStackTrace();
    } catch (IOException e) {

    e.printStackTrace();
    }
    clip.start();
    }
    else if(source.getLabel() == "3-10"){
    File file = new File("C:\\Users\\Snehitha\\Desktop\\10.AU");
    try {
    song = AudioSystem.getAudioInputStream(file);
    } catch (UnsupportedAudioFileException e) {

    e.printStackTrace();
    } catch (IOException e) {

    e.printStackTrace();
    }
    try {
    clip=AudioSystem.getClip();
    } catch (LineUnavailableException e) {

    e.printStackTrace();
    }
    try {
    clip.open(song);
    } catch (LineUnavailableException e) {

    e.printStackTrace();
    } catch (IOException e) {

    e.printStackTrace();
    }
    clip.start();
    }
    else if(source.getLabel() == "3-11"){
    File file = new File("C:\\Users\\Snehitha\\Desktop\\11.AU");
    try {
    song = AudioSystem.getAudioInputStream(file);
    } catch (UnsupportedAudioFileException e) {

    e.printStackTrace();
    } catch (IOException e) {

    e.printStackTrace();
    }
    try {
    clip=AudioSystem.getClip();
    } catch (LineUnavailableException e) {

    e.printStackTrace();
    }
    try {
    clip.open(song);
    } catch (LineUnavailableException e) {

    e.printStackTrace();
    } catch (IOException e) {

    e.printStackTrace();
    }
    clip.start();
    }
    else if(source.getLabel() == "3-12"){
    File file = new File("C:\\Users\\Snehitha\\Desktop\\0.AU");
    try {
    song = AudioSystem.getAudioInputStream(file);
    } catch (UnsupportedAudioFileException e) {

    e.printStackTrace();
    } catch (IOException e) {

    e.printStackTrace();
    }
    try {
    clip=AudioSystem.getClip();
    } catch (LineUnavailableException e) {

    e.printStackTrace();
    }
    try {
    clip.open(song);
    } catch (LineUnavailableException e) {

    e.printStackTrace();
    } catch (IOException e) {

    e.printStackTrace();
    }
    clip.start();
    }
    }

    public JComponent getMainPanel() {
    return mainPanel;
    }

    private class MyComponentAdapter extends ComponentAdapter {
    @Override
    public void componentResized(ComponentEvent e) {
    Dimension size = mainPanel.getSize();
    int blackShift = size.width / (BLACK_KEY_GRID_ROW_COUNT * 2);
    blackKeyPanel.setLocation(blackShift, 0);

    whiteKeyPanel.setSize(size);
    blackKeyPanel.setSize(new Dimension(size.width/2, size.height/2));

    mainPanel.revalidate();
    mainPanel.repaint();
    }
    }



    private static void createAndShowUI() {
    JFrame frame = new JFrame("PianoKeys");
    frame.getContentPane().add(new help().getMainPanel());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    }

    public static void main(String[] args) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    createAndShowUI();
    }

    });

    }
    }

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    That's a heck of lot of unformatted code.

    Consider:

    1) Doing some debugging using println statements or a debugger to check the state of your classes at critical times.
    2) If still stuck, post a small compilable program that demonstrates your problem and that uses [cod&#101;] [/cod&#101;] tags so that the code retains its formatting and is actually readable.

    Regardless, please let us know if you fix this problem. Much luck.

Similar Threads

  1. Image into Grid layout
    By Jaxn77 in forum AWT / Swing
    Replies: 6
    Last Post: 02-10-2011, 03:02 PM
  2. Image into Grid layout
    By Jaxn77 in forum Java Applets
    Replies: 1
    Last Post: 02-10-2011, 03:18 AM
  3. JFrame: JButtons in a grid
    By jackal in forum New To Java
    Replies: 2
    Last Post: 06-09-2010, 07:56 PM
  4. Grid layout frames GUI
    By fritz1474 in forum AWT / Swing
    Replies: 1
    Last Post: 10-15-2008, 02:04 AM
  5. Help with Grid Layout
    By coco in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 08:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •