1

I have a problem with jLayer. I have a button with code for starting a song:

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {        
    try {
        Player prehravac;
        FileInputStream buff = new FileInputStream(Okno.filename);
        prehravac = new Player(buff);
        prehravac.play();
        if (prehravac != null) {
            prehravac.close();
            this.dispose();
        }
    } catch(Exception e) {

    }
}                                    

When I click on this button, it begins to play the song but whole application freezes and I cannot click on anything. When the song ends, it's okay and I can click on other components again.

Can someone help me pls ? :) thank

4

2 回答 2

3

这是因为歌曲播放发生在与 GUI 相同的线程中,或者更具体地说是EDT。因此,当歌曲播放时,GUI 没有响应。要解决此问题,请执行以下操作:

new Thread(){
  run(){
    //Your play code
  }
}.start();

这将在一个单独的线程中播放你的东西。但是,您必须注意,由于 GUI 是响应式的,因此您可以在歌曲仍在播放时开始播放歌曲。

有关更多信息,请参阅

于 2013-06-09T19:50:19.013 回答
2

(事件调度线程)上的任务必须快速完成;如果他们不这样做,未处理的事件会备份并且用户界面变得无响应。EDT

于 2013-06-09T19:52:20.853 回答