标签云

微信群

扫码加入我们

WeChat QR Code

I have constructed a simple program to play a song.import javafx.scene.media.Media;import javafx.scene.media.MediaPlayer;import java.io.File;import javax.swing.*;import java.awt.*;import javafx.embed.swing.JFXPanel;import javafx.scene.Scene;import javafx.application.Platform;import javafx.scene.Group; public class Fade {private void initAndShowGUI() {// This method is invoked on Swing threadJFrame frame = new JFrame("FX");final JFXPanel fxPanel = new JFXPanel();frame.add(fxPanel);frame.setVisible(true);Platform.runLater(new Runnable() {@Overridepublic void run() {initFX(fxPanel);}});}private void initFX(JFXPanel fxPanel) {// This method is invoked on JavaFX threadGroup root = new Group(); Scene scene = new Scene(root, 500, 500, true);fxPanel.setScene(scene);try {String bip = new File("./Sound/Welcome To The Show - L1-5.mp3").toURI().toString();Media hit = new Media(bip);MediaPlayer mediaPlayer = new MediaPlayer(hit);mediaPlayer.play();Thread.sleep(10000);System.out.println("Fade");} catch (Exception e) {System.out.println("Exception");System.out.println(e);}}public static void main(String[] args) {SwingUtilities.invokeLater(new Runnable() {@Overridepublic void run() {new Fade().initAndShowGUI();}});}}However when this code is run the Thread.sleep(10000) runs before the song starts playing instead of the song starting playing then 10 seconds later fade is printed out to the console. How may I fix this?