如何在以下代码的两个不同线程中运行ActionListenters

时间:2013-01-15 13:15:56

标签: java swing awt scope actionlistener

我让这个程序在java中播放mp3文件。  以下代码播放mp3文件,但播放时无法停止... 我将两个单独的ActionListener添加到两个按钮,但它不适用于停止按钮......

import java.io.*;    
import javax.swing.*;
import java.awt.event.*;
import javazoom.jl.player.Player;  
import java.io.FileInputStream;



    public class Play2 extends JFrame
    {  
    JButton b,b1;
    JTextField t;

    Play2()
    {
        JFrame j=new JFrame("MusicPlayer");
        j.setSize(300,300);
        j.setDefaultCloseOperation(EXIT_ON_CLOSE);
        b=new JButton("Play");
        b1=new JButton("Stop");
        JPanel p=new JPanel();
        t=new JTextField(20);
        p.add(t);
        p.add(b);
        p.add(b1);
        j.add(p);
        j.setVisible(true);


    }
    public void awt()
    {
    b.addActionListener(
            new ActionListener(){
                  public void actionPerformed(ActionEvent ae){
                try  
                    {  

            String fname=t.getText();
            File directory = new File(fname);

            boolean isDirectory = directory.isDirectory();

            if (isDirectory) 
             {
                    // It returns true if directory is a directory.
                System.out.println("the name you have entered is a directory  : "  +  directory);  
                    //It returns the absolutepath of a directory.
                    System.out.println("the path is "  +  directory.getAbsolutePath());
             }
            else
            {
                    // It returns false if directory is a file.
                System.out.println("the name you have entered is a file  : " + directory);
                    //It returns the absolute path of a file.
                    System.out.println("the path is "  + directory.getAbsolutePath());
            }
                String s=directory.getAbsolutePath();

                s=s.replace("\\","/") ;

                    FileInputStream fis=new FileInputStream(s);  
                    final Player playMp3=new Player(fis);  

                    playMp3.play(); 
        }
        catch(Exception e){
            System.out.println(e);}
        }//end actionPerformed
      }//end ActionListener
    );//end addActionListener()

    b1.addActionListener(
      new ActionListener(){
        public void actionPerformed(
                                  ActionEvent ae){
          //Terminate playback before EOF
                    try  
                    {  

            String fname=t.getText();
            File directory = new File(fname);

            boolean isDirectory = directory.isDirectory();

            if (isDirectory) 
             {
                    // It returns true if directory is a directory.
                System.out.println("the name you have entered is a directory  : "  +  directory);  
                    //It returns the absolutepath of a directory.
                    System.out.println("the path is "  +  directory.getAbsolutePath());
             }
            else
            {
                    // It returns false if directory is a file.
                System.out.println("the name you have entered is a file  : " + directory);
                    //It returns the absolute path of a file.
                    System.out.println("the path is "  + directory.getAbsolutePath());
            }
                String s=directory.getAbsolutePath();

                s=s.replace("\\","/") ;

                    FileInputStream fis=new FileInputStream(s);  
                    final Player playMp3=new Player(fis);  

                    playMp3.close(); 
        }
        catch(Exception e){
            System.out.println(e);}
        }//end actionPerformed
      }//end ActionListener
    );//end addActionListener()

    }


        public static void main(String[]args)  
        {  
        Play2 f=new Play2();
        f.awt();    
    }

}  

1 个答案:

答案 0 :(得分:0)

你在这里做的是用你的播放按钮监听器来占用EDT(Event Dispatch Thread)。

单击“播放”按钮时,ActionEvent将发布到“事件队列”。如果您随后按下“停止”按钮,则会将另一个ActionEvent发布到EventQueue。

EDT将开始处理事件队列中的第一个事件(播放事件)。在那种情况下,您在EDT上的(mp3)play()类上调用Player方法。 play()方法一直循环,直到整个mp3播放完毕。在整个时间内,另一个事件(停止事件)将在事件队列中等待轮到执行。因此,当它最终成为停止事件的执行时,mp3已经被播放直到结束,并且停止命令相当多余。


主要问题:您不应该在EDT上执行冗长的任务。

解决方案:确保使用SwingWorker class调用Player.play()方法在非EDT线程上执行play方法。

参考Event-Dispatch Thread Rules for Swing UIs (online article)(您可能会对此感兴趣。特别是从不延迟EDT 部分中的信息,其中包含有关的信息这个特殊的问题)