【问题标题】:How can I get the Jbutton wait until the music ends?如何让 Jbutton 等到音乐结束?
【发布时间】:2013-12-08 10:14:40
【问题描述】:
        Playjb.addActionListener(new ActionListener(){

         @Override
         public void actionPerformed(ActionEvent e)
        {
             try {
                 InputStream in;
                 Path = rs.getString("Path");
                 in = new FileInputStream(new File(Path));
                 AudioStream audios=new AudioStream(in);
                 AudioPlayer.player.start(audios);
                 System.out.println("play");  

                 //here I want to add somthing that sets the OKjb 
                 //(another JButton) inactive until the audio is finished.//

             }catch(exception e){

             }

【问题讨论】:

  • 放入SwingWorker,在done()方法中启用按钮。
  • 对不起,我没听懂。请你写下来好吗?
  • @user3026191 如果对您有帮助,请考虑接受答案或要求更多说明。

标签: java swing audio jbutton wait


【解决方案1】:

使用 JButton 的 setEnabled() 方法并将值 false 传递给它以禁用它。当音乐结束时,再次调用它并将值传递给 true 以再次启用它。

音乐应在后台播放,因此请按照建议使用 SwingWorker。在 doInBackground() 中禁用按钮并在 done() 中启用它

永远记住不要将需要很长时间才能执行的代码放在侦听器的回调方法中。这些方法只是让您知道事件发生的中继。任何长时间运行的任务都必须卸载到后台。

【讨论】:

  • 当然。我可以做到,但你知道怎么做吗?
  • 还没有!如果你能做到,我将不胜感激!
  • 当然,只要我完成了我的大学工作。几个小时 =]
猜你喜欢
  • 2016-01-09
  • 2017-07-04
  • 1970-01-01
  • 1970-01-01
  • 2015-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-09
相关资源
最近更新 更多