1

私の単純な Java プロジェクトとして音楽プレーヤーを作成する必要があります。ファイルを開き、名前をテキスト フィールドに読み込みます。再生を押すと、名前がテキスト フィールドに囲まれ、一時停止を押すと、囲みが一時停止します。再生の一時停止は JTogglebutton で、ボタン名は stop です。マルチスレッドを使用して、文字列を再生および一時停止することはできますが、停止を 1 回押した後、もう一度新しい音楽ファイルを開いて再生を押すと、違法なスレッド状態例外が表示されます。ここで私を助けてください..注: まだ入れていませんその中で音楽を再生するためのコードで..この問題が解決したら、それを入れます。ありがとうございます

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();
    }

}
4

2 に答える 2

0

Thread.stop()Thread.suspend()またはを使用しないでくださいThread.resume()。これらのメソッドは非推奨であり、深刻な影響 (デッドロックの可能性など) があることが明示的に文書化されています。Thread.stop、Thread.suspend、Thread.resume が推奨されない理由も参照してください。.

start()この特定のケースでは、Thread複数回実行できないため、例外が発生します。の非推奨で危険なメソッドを使用せずに、スレッドがアプリケーションの一時停止、開始、停止などを正しく処理するように、スレッドをコーディングする必要があります (または、拡張しないThreadで実装する必要があります) 。RunnableThread

于 2013-03-07T09:12:11.440 に答える
0

このエラーが発生する理由は、実行中のスレッドの状態を手動で操作しようとしているためです。

ここで使用しているメソッドはすべて非推奨ですstop, resume, suspend- これらはすべて避けるべきです。

プログラムがどのように実行されているかを考えるより良い方法は、スレッドが生きているか生きていないかということです。生きている間は実際のメソッド内で何もしないかもしれませんがrun、実際のスレッドを一時停止/一時停止/停止することは決してありません-アプリケーションロジックを一時停止/停止しているだけです。

あなたが見ているものに対する非常に簡単な修正は、すべての停止/一時停止/再開を次のものに置き換えることです:

private volatile boolean paused = false;
private void pausePlayer(boolean pause) {
    this.paused = pause;
}

then everywhere you need to pause the player use:
pausePlayer(true);

and when you want to resume it use:
pausePlayer(false);

run メソッドで、2 番目の for ループを次のように置き換えます。

for (; ; ) {
    while(!paused) {
        // do your work

ただし、現状のプログラムにはより大きな構造上の問題があると私は信じていることに注意してください。通常のスレッドと Swing を混在させると問題が発生する可能性があります。代わりにSwingWorkerを使用して調査することをお勧めします。これは、あなたがしようとしていることのために明示的に設計されているためです。

于 2013-03-07T09:49:28.190 に答える