如何通过按 Java Swing 按钮来停止线程?

How to stop a Thread by pressing a Java Swing button?

我创建了一个线程,它播放来自已实现的 Runnable class(Soundtrack) 的歌曲,我想通过按下我的按钮 (jMenuItem1ActionPerformed) 来停止它。我用谷歌搜索并尝试了很多方法来停止但失败了我认为在我的情况下还有另一种方法可以做到这一点。以下代码如下:

public static class Soundtrack implements Runnable {
    @Override
    public void run() {
        try{
        File file = new File("SF.mp3");
        FileInputStream fis = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(fis);

        try{
            Player player = new Player(bis);
            player.play();
        }catch(JavaLayerException ex){}
    }catch(IOException e){}
    }
    }
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
    // TODO add your handling code here:
}    
public static void main(String args[]) {
    Thread background = new Thread(new Soundtrack());
    background.start();
}  

谢谢姐姐的解答:

public static class Soundtrack implements Runnable {
    @Override
    public void run() {
        try{
        File file = new File("SF.mp3");
        FileInputStream fis = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(fis);

        try{
            Player player = new Player(bis);
            player.play();
        }catch(JavaLayerException ex){}
    }catch(IOException e){}
    }
    }
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
    // TODO add your handling code here:
        background.stop();     
}                                          
private static Thread background;
public static void main(String args[]) {
    background = new Thread(new Soundtrack());
    background.start();
}