繁体   English   中英

使用我的JFrame同时播放音乐

[英]Play music on the same time with my JFrame

在我的应用程序中,我向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();
}
}

我以前没有使用Clip ,但是您应该在单独的线程上播放音乐,因为您不应该在UI线程上执行长时间运行的任务(如您所见,它会冻结UI):

public void run() 
{

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM