2013-03-04 89 views
-2

你好,我想讓ProgressBar加載,直到歌曲開始... 我有這段代碼,但ProgressBar開始時,歌曲開始,它的製作完成約5秒。所以請別人幫我做正確的...謝謝:)當進度條正在運行時阻塞線程

package passwordsaver1; 
import java.awt.Dimension; 
import java.awt.Toolkit; 
import java.net.URL; 
import javax.sound.sampled.AudioInputStream; 
import javax.sound.sampled.AudioSystem; 
import javax.sound.sampled.Clip; 
import javax.swing.JOptionPane; 

public final class Music extends javax.swing.JFrame { 

void centreWindow() { 
    Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize(); 
    int x = (int) ((dimension.getWidth() - getWidth())/2); 
    int y = (int) ((dimension.getHeight() - getHeight())/2); 
    setLocation(x, y); 
} 

public Music(String mainAccName) { 
    initComponents(); 
    centreWindow(); 
    setTitle("PasswordSaver - Music"); 
    this.mainAccName = mainAccName; 
} 
String mainAccName; 

private void pitbullActionPerformed(java.awt.event.ActionEvent evt) {           
    try { 
     URL url = new URL("http://mini-mk-market.com/music/PitBulll.wav"); 
     Clip clip = AudioSystem.getClip(); 
     AudioInputStream ais; 
     ais = AudioSystem.getAudioInputStream(url); 
     clip.open(ais); 
     clip.loop(5); 
     clip.start(); 
     new Thread(new Start()).start(); 
    } catch (Exception ex) { 
     JOptionPane.showMessageDialog(null, "Can't find Internet Connection !"); 
    } 
} 

// this is just for testing does the song will stop after clicking the new... but cant.. 
private void afrojackActionPerformed(java.awt.event.ActionEvent evt) { 
    try { 
     URL url = new URL("http://mini-mk-market.com/music/PitBulll.wav"); 
     Clip clip = AudioSystem.getClip(); 
     AudioInputStream ais; 
     ais = AudioSystem.getAudioInputStream(url); 
     clip.open(ais); 
     clip.loop(0); 
     clip.start(); 
     new Thread(new Start()).start(); 
    } catch (Exception ex) { 
     JOptionPane.showMessageDialog(null, "Can't find Internet Connection !"); 
    } 
} 

public static void main(String args[]) { 
    try { 
       for (javax.swing.UIManager.LookAndFeelInfo info: 
       javax.swing.UIManager.getInstalledLookAndFeels()) { 
       if ("Nimbus".equals(info.getName())) { 
       javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
       break; 
      } 
     } 
    } catch (ClassNotFoundException ex) { 
     java.util.logging.Logger.getLogger(Music.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(Music.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(Music.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(Music.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } 
    //</editor-fold> 

    /* Create and display the form */ 
    java.awt.EventQueue.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
      //new Music().setVisible(true); 
     } 
    }); 
} 

private class Start implements Runnable { 

    @Override 
    public void run() { 
     for (int x = 0; x < 101; x++) { 
      ProgressBar.setValue(x); 
      ProgressBar.repaint(); 
      try { 
       Thread.sleep(50); 
      } catch (Exception ex) { 
      } 
     } 
    } 
} 
+5

這是一個很多的代碼。你可以把它歸結爲一個能證明問題的最小例子嗎?例如,外觀和窗口居中的東西都不直接相關。 – 2013-03-04 21:12:33

+3

不要從Event Dispatching Thread外部更新/創建任何UI組件! – MadProgrammer 2013-03-04 21:20:09

+0

給我看一些exsample我不能得到它像這樣...:S – user2133393 2013-03-04 21:26:30

回答