需要制作一个音乐播放器作为我的简单 java 项目。它打开一个文件并在文本字段中加载名称。当按下播放时,名称被包围在文本字段中,当按下暂停时,包围暂停。播放暂停是 JTogglebuttons,我有一个按钮名称停止。使用多线程,我可以播放和暂停字符串,但是在我按一次 Stop 之后.. 如果我再次打开一个新的音乐文件并按播放,它会显示非法线程状态异常。请在这里帮帮我.. 注意:我还没有放在其中播放音乐的代码中..一旦解决了这个问题,我就会把它放进去。非常感谢你
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import sun.audio.*;
public class search extends Thread implements ActionListener
{
JFrame f;
JButton stop,open;
JToggleButton play;
JTextField tf,text;
JTextArea ta;
JLabel lab,lab1;
String str,dest;
JScrollPane scrol;
File fl;
int myfl=0,myfl1=0,myfl2=0;
search()
{
f=new JFrame("Music Player");
f.setLayout(null);
f.setSize(620,300);
play=new JToggleButton("play");
play.setBounds(100,150,270,30);
play.addActionListener(this);
f.add(play);
play.setEnabled(false);
stop=new JButton("stop");
stop.setBounds(400,150,120,30);
stop.addActionListener(this);
f.add(stop);
stop.setEnabled(false);
open=new JButton("open");
open.setBounds(100,200,420,30);
open.addActionListener(this);
f.add(open);
tf=new JTextField();
tf.setBounds(25,50,565,40);
tf.setFont(new Font("DK BabySitter",Font.BOLD,20));
tf.setHorizontalAlignment(JTextField.CENTER);
f.add(tf);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand().equals("open"))
{
FileDialog fd=new FileDialog(f,"Open Box",FileDialog.LOAD);
fd.setSize(300,300);
fd.setVisible(true);
String s1="mp3";
str=fd.getFile();
dest=fd.getDirectory()+fd.getFile();
if(str.toLowerCase().endsWith(s1))
{
tf.setText(str);
//pause.setEnabled(true);
play.setEnabled(true);
stop.setEnabled(true);
}
else
{
JOptionPane.showMessageDialog(f, "Select a valid file format");
}
}
if(ae.getActionCommand().equals("stop"))
{
play.setLabel("play");
myfl1=1;
tf.setText(" ");
stop();
}
if(ae.getActionCommand().equals("play"))
{
try
{
play.setLabel("pause");
if(myfl==1 && myfl1==0)
{
resume();
}
if(myfl==0 || myfl1==1)
{
start();
}
}
catch(IllegalThreadStateException e)
{
tf.setText("error a gya re");
Thread newth= new Thread();
newth.start();
}
}
if(ae.getActionCommand().equals("pause"))
{
play.setLabel("play");
myfl=1;
suspend();
}
}
public void run()
{
try
{
String rot=tf.getText();
char rotn[]=new char[rot.length()];
int flag=rot.length();
int move=0;
rotn=rot.toCharArray();
tf.setText(" ");
for(;;)
{
for(;;)
{
sleep(100);
tf.setText( tf.getText() + rotn[move]);
move++;
if(move==(flag-1))
{
move=0;
break;
}
}
tf.setText(" ");
}
}
catch(Exception e)
{
tf.setText("error occured");
}
}
public static void main(String args[])
{
try {
// Set System L&F
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
}
catch (UnsupportedLookAndFeelException e) {
// handle exception
}
catch (ClassNotFoundException e) {
// handle exception
}
catch (InstantiationException e) {
// handle exception
}
catch (IllegalAccessException e) {
// handle exception
}
new search();
}
}