0
在我的應用程序中,我添加了一些音樂到我的JFrame主頁。現在,當我在我的主體上運行它時,他沒有任何問題,他打開了主頁JFrame,同時音樂正在播放。與我的JFrame同時播放音樂
import Muziek.Sound;
import View.Home;
public class Main {
public static void main(String[] args) {
Home home = new Home();
home.setVisible(true);
}
}
但現在當我運行我的家庭類,而正在播放的音樂,當音樂停止我可以看到我的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();
}
}
使用'Clip'如圖所示[這裏](http://stackoverflow.com/a/9656682/418556)(即代碼播放在2個剪輯同一時間 - 嗚呼!)。 – 2012-03-14 12:16:00