0

//here is a copy my code that i am trying to make play eye of the tiger, which i have saved on my desktop. first where should i save it, also how do i make it play? and also does it matter what type of file it is (i am using a mp3) any help would be appreciated. also if you could explain the code that would help (music is also saved to eclipse < java < stopwatch < src < stopwatch )

package stopwatch;


 import java.awt.Container;
 import java.awt.GridBagConstraints;
 import java.awt.GridBagLayout;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.util.Date;
 import javax.swing.JButton;
 import javax.swing.JFrame;
 import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.plaf.ColorUIResource;

import org.omg.CORBA.portable.InputStream;

 public class StopWatch
   extends JFrame
 {
/**
     * 
     */
    private static final long serialVersionUID = -7203812812064067525L;
   long startTime;
   long stopTime;
   long currentTime;
   double elapsedTime;
   boolean timeStarted = false;
   int lapTime;

   JButton startButton = new JButton();
   JLabel startLabel = new JLabel();
   JTextField startTextField = new JTextField();

   JButton lapButton = new JButton();
   JLabel lapLabel = new JLabel();
   JTextField lapTextField = new JTextField();

   JButton stopButton = new JButton();
   JLabel stopLabel = new JLabel();
   JTextField stopTextField = new JTextField();

   JButton exitButton = new JButton();
   JLabel elapsedLabel = new JLabel();
   JTextField elapsedTextField = new JTextField();

   public StopWatch(String title)
   {






       setTitle(title);
     setDefaultCloseOperation(3);


     getContentPane().setLayout(new GridBagLayout());
     GridBagConstraints gridConstraints = new GridBagConstraints();


     this.startButton.setText("Start Timing");
     gridConstraints.gridx = 0;
     gridConstraints.gridy = 0;
     getContentPane().add(this.startButton, gridConstraints);


     this.startButton.addActionListener(new ActionListener()
     {
       public void actionPerformed(ActionEvent e)
       {
         StopWatch.this.startButtonActionPerformed(e);
       }
     });
     this.startLabel.setText("  Start System Time  ");
     gridConstraints.gridx = 1;
     gridConstraints.gridy = 0;
     getContentPane().add(this.startLabel, gridConstraints);

     this.startTextField.setText("");
     this.startTextField.setColumns(20);
     gridConstraints.gridx = 2;
     gridConstraints.gridy = 0;
     getContentPane().add(this.startTextField, gridConstraints);

     this.lapButton.setText("Lap");
     gridConstraints.gridx = 0;
     gridConstraints.gridy = 1;
     getContentPane().add(this.lapButton, gridConstraints);

     this.lapButton.addActionListener(new ActionListener()
     {
       public void actionPerformed(ActionEvent e)
       {
         StopWatch.this.lapButtonActionPerformed(e);
       }
     });
     this.lapLabel.setText("  Lap System Time  ");
     gridConstraints.gridx = 1;
     gridConstraints.gridy = 1;
     getContentPane().add(this.lapLabel, gridConstraints);


     this.lapTextField.setText("");
     this.lapTextField.setColumns(20);
     gridConstraints.gridx = 2;
     gridConstraints.gridy = 1;
     getContentPane().add(this.lapTextField, gridConstraints);



     this.stopButton.setText("Stop Timing");
     gridConstraints.gridx = 0;
     gridConstraints.gridy = 2;
     getContentPane().add(this.stopButton, gridConstraints);

     this.stopButton.addActionListener(new ActionListener()
     {
       public void actionPerformed(ActionEvent e)
       {
         StopWatch.this.stopButtonActionPerformed(e);
       }
     });
     this.stopLabel.setText("  Stop System Time  ");
     gridConstraints.gridx = 1;
     gridConstraints.gridy = 2;
     getContentPane().add(this.stopLabel, gridConstraints);

     this.stopTextField.setText("");
     this.stopTextField.setColumns(20);
     gridConstraints.gridx = 2;
     gridConstraints.gridy = 2;
     getContentPane().add(this.stopTextField, gridConstraints);



     this.exitButton.setText("Close ALL Watches");
     gridConstraints.gridx = 0;
     gridConstraints.gridy = 3;
     getContentPane().add(this.exitButton, gridConstraints);

     this.exitButton.addActionListener(new ActionListener()
     {
       public void actionPerformed(ActionEvent e)
       {
         StopWatch.this.exitButtonActionPerformed(e);
       }
     });



     this.elapsedLabel.setText("  Elapsed Time Seconds  ");
     gridConstraints.gridx = 1;
     gridConstraints.gridy = 3;
     getContentPane().add(this.elapsedLabel, gridConstraints);

     this.elapsedTextField.setText("");
     this.elapsedTextField.setColumns(20);
     gridConstraints.gridx = 2;
     gridConstraints.gridy = 3;
     getContentPane().add(this.elapsedTextField, gridConstraints);



     setLocationRelativeTo(null);

     pack();
   }

   private void startButtonActionPerformed(ActionEvent e)
   {
     this.startTime = System.currentTimeMillis();


     Date startDate = new Date();
     this.startTextField.setText(startDate.toString());

     this.stopTextField.setText("");
     this.elapsedTextField.setText("");
     this.timeStarted = true;
     this.lapTextField.setText("");
   }

   private void lapButtonActionPerformed(ActionEvent e)
   {
         if (this.timeStarted)
         {
           this.currentTime = System.currentTimeMillis();


           this.elapsedTime = ((this.currentTime - this.startTime) / 1000.0D);
           this.lapTextField.setText(String.valueOf(this.elapsedTime));

         }
         else {}
       }

   private void stopButtonActionPerformed(ActionEvent e)
   {
     if (this.timeStarted)
     {
       this.stopTime = System.currentTimeMillis();
       Date stopDate = new Date();
       this.stopTextField.setText(stopDate.toString());

       this.elapsedTime = ((this.stopTime - this.startTime) / 1000.0D);
       this.elapsedTextField.setText(String.valueOf(this.elapsedTime));

       timeStarted=(false);
     }
     else {}
   }

   private void exitButtonActionPerformed(ActionEvent e)
   {
     System.exit(0);
   }
 }
  • what exactly is the problem? what have you tried? what do you expect? you should narrow down the above code snippet to the [minimal](http://stackoverflow.com/help/mcve) pieces that explain the problem. – Andrea Jan 31 '14 at 19:59
  • i have tried looking at other questions but none were very helpful, the problem is that the sound wont play but the code wont crash – user3258646 Jan 31 '14 at 20:04
  • 1
    the only code here seems to be for rendering buttons/textfields/labels, which is irrelevant to the question at hand. maybe [this question](http://stackoverflow.com/q/6045384/640030) has what you want...? – Andrea Jan 31 '14 at 20:23

1 Answers1

1

To play MP3's in java you'd need to add an MP3 library to your program. I normally use javazoom for that. Otherwise you could convert it to a wav file and use sound buffering.

http://www.javazoom.net/javalayer/javalayer.html

kayleighsdaddy
  • 670
  • 5
  • 15