【问题标题】:How to run ActionListenters in two different thread for the following code如何为以下代码在两个不同的线程中运行 ActionListenters
【发布时间】:2012-12-29 14:27:48
【问题描述】:

我制作了这个程序来在 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();    
    }

}  

【问题讨论】:

  • final Player playMp3=new Player(fis); playMp3.play(); playMp3 应该声明为类的一个属性,该属性可用于任何需要它的方法,例如start()stop() 方法。
  • 请使用一致且连贯的缩进。就目前而言,您的代码很难阅读(而且很容易出错)!

标签: java swing awt scope actionlistener


【解决方案1】:

您在这里所做的是使用播放按钮侦听器占用 EDT(事件调度线程)。

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

EDT 将开始处理事件队列中的第一个事件(播放事件)。在这种情况下,您在 EDT 上的 (mp3) Player 类上调用 play() 方法。 play() 方法不断循环,直到播放完整个 mp3。在这整个时间内,另一个事件(停止事件)将在事件队列中等待轮到它执行。所以当最终轮到stop事件执行时,mp3已经播放到最后,stop命令比较多余。


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

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

参考Event-Dispatch Thread Rules for Swing UIs (online article)(您可能会感兴趣阅读此内容。尤其是 Never Delay the EDT 部分中的信息,其中包含有关此特定信息的信息问题)

【讨论】:

    猜你喜欢
    • 2012-12-04
    • 2021-08-21
    • 1970-01-01
    • 2021-02-16
    • 1970-01-01
    • 2022-10-05
    • 2022-01-07
    • 2018-06-02
    • 2022-11-11
    相关资源
    最近更新 更多