//这是我的代码副本,我正在尝试制作老虎之眼,我已将其保存在桌面上。首先我应该把它保存在哪里,还有我该如何让它播放?
它是什么类型的文件也有关系吗(我使用的是 mp3)
任何帮助,将不胜感激。另外如果你能解释一下有帮助的代码
(音乐也保存到eclipsepackage 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);
}
}
最佳答案
要在 Java 中播放 MP3,您需要将 MP3 库添加到您的程序中。我通常使用 javazoom 来实现这一点。否则,您可以将其转换为 wav 文件并使用声音缓冲。
关于java - java eclipse 中的 mp3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21488540/