使用我的JFrame同时播放音乐

时间:2012-03-14 12:05:36

标签: java eclipse multithreading

在我的应用中,我已经为我的JFrame家添加了一些音乐。现在,当我在我的主电源上运行时,没有问题他打开Home JFrame,同时播放音乐。

import Muziek.Sound;
import View.Home;

public class Main {


    public static void main(String[] args) {
    Home home = new Home(); 
    home.setVisible(true);

    }
}

但是现在当我运行我的Home课程时,JFrame在音乐播放时变为白色,当音乐停止时我可以看到我的JFrame。现在我已经阅读了一些关于线程的内容,所以你可以在同一时间运行它,但我现在不知道如何做到这一点。

package View;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.io.File;


import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;

import javax.swing.JPanel;

import Controller.HomeController;
import Muziek.Sound;





public class Home extends JFrame {

    private JLabel label, label1, label2;
    private JPanel panel;
    private JButton logo, logo1, logo2, logo3, logo4, logo5, selectie;
    private Container window = getContentPane();
    private HomeController Controller;

    public Home (){
        initGUI();
        Sound sound = new Sound();
        sound.play();
    }
    public void addHomeListener(ActionListener a){
        selectie.addActionListener(a);
    }
    public void initGUI(){
        setLayout(null);
        setTitle("");
        setPreferredSize(new Dimension(800,600));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        label = new JLabel();       
        label.setBounds(0, 0, 266, 800);
        label.setBackground(Color.WHITE);
        label.setOpaque(true);
        window.add(label);

        label1 = new JLabel();
        label1.setBounds(267, 0, 266, 800);
        label1.setBackground(Color.RED);
        label1.setOpaque(true);
        window.add(label1);

        label2 = new JLabel();
        label2.setBounds(533, 0, 266, 800);
        label2.setBackground(Color.WHITE);
        label2.setOpaque(true);
        window.add(label2);

        logo = new JButton(new ImageIcon("../Ajax/src/img/logotje.gif"));
        logo.setBorderPainted(false);
        logo.setBounds(40, 150, 188, 188);
        label1.add(logo);

        logo1 = new JButton(new ImageIcon("../Ajax/src/img/Ster.png"));
        logo1.setBorderPainted(false);
        logo1.setBounds(10, 50, 82, 82);
        label1.add(logo1);

        logo2 = new JButton(new ImageIcon("../Ajax/src/img/Ster.png"));
        logo2.setBorderPainted(false);
        logo2.setBounds(92, 20, 82, 82);
        label1.add(logo2);

        logo3 = new JButton(new ImageIcon("../Ajax/src/img/Ster.png"));
        logo3.setBorderPainted(false);
        logo3.setBounds(174, 50, 82, 82);
        label1.add(logo3);

        logo4 = new JButton(new ImageIcon("../Ajax/src/img/shirt.png"));
        logo4.setBorderPainted(false);
        logo4.setBounds(50, 50, 135, 182);
        label.add(logo4);

        logo5 = new JButton(new ImageIcon("../Ajax/src/img/uitshirt.png"));
        logo5.setBorderPainted(false);
        logo5.setBounds(65, 50, 138, 190);
        label2.add(logo5);

        selectie = new JButton("Selectie");
        selectie.setBounds(60, 500, 99, 25);
        selectie.setActionCommand("selectie");
        label.add(selectie);

        pack();

           Controller = new HomeController(this);
            addHomeListener(Controller);

            setVisible(true);
        }

    public static void main(String... args)
    {
        javax.swing.SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new Home();
            }
        });
    }
}

我的课堂音乐

package Muziek;

import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JOptionPane;

public class Sound implements Runnable  {


private File soundFile;
private Clip clip;
private Runnable play;

public Sound(File soundFile){
this.soundFile = soundFile;
}

public Sound() {
    soundFile = new File("../Ajax/src/sound/Sound1.wav");
    new Thread(play).start();

}

public void prepare(){

try {
AudioInputStream soundIn = AudioSystem.getAudioInputStream(soundFile);
AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
AudioSystem.NOT_SPECIFIED,
16,2,4,
AudioSystem.NOT_SPECIFIED, true);
DataLine.Info info = new DataLine.Info(Clip.class, format);

clip = (Clip)AudioSystem.getLine(info);
clip.open(soundIn);

}catch(IOException e){
JOptionPane.showMessageDialog(null, e.getMessage());
}
catch(UnsupportedAudioFileException e){
JOptionPane.showMessageDialog(null, e.getMessage());
}
catch(LineUnavailableException e){
JOptionPane.showMessageDialog(null, e.getMessage());
}

}

public void run() 
{

}

public void play(){
prepare();
clip.start();
while(clip.isRunning()){
Thread.yield();
}

}
public void stop(){
clip.stop();
}
}

1 个答案:

答案 0 :(得分:1)

之前我没有使用Clip,但是你应该在一个单独的线程上播放音乐,因为你不应该在UI线程上执行长时间运行的任务(它会冻结UI,你可以看到) :

public void run() 
{

    prepare();
    clip.start();
    // ...
}

public void play(){
    new Thread(this).start();
}