Need help having my buttons play songs when clicked. package jukebox; import jav
ID: 3789454 • Letter: N
Question
Need help having my buttons play songs when clicked.
package jukebox;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MusicFrame extends JFrame {
private Title title;
private Songs song;
public MusicFrame(){
setTitle("Dean's Fabulous Jukebox");
title = new Title();
song = new Songs();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(700, 900);
setLayout(new FlowLayout(FlowLayout.CENTER));
add(title, BorderLayout.NORTH);
add(song, BorderLayout.SOUTH);
setVisible(true);
}
}
////////////////////////
package jukebox;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Title extends JPanel {
public Title(){
Icon icon = new ImageIcon(getClass().getResource("Images/DJ.jpg"));
JLabel label1 = new JLabel("Dean's JukeBox", icon, JLabel.LEFT);
add(label1);
}
}
////////////////
package jukebox;
import java.awt.GridLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
public class Songs extends JPanel{
public Songs(){
setLayout(new GridLayout(1,2));
Icon Song1Icon = new ImageIcon(getClass().getResource("Images/Down.jpg"));
JButton song1 = new JButton("Don't Let Me Down", Song1Icon);
add(song1);
Icon Song2Icon = new ImageIcon(getClass().getResource("Images/Paris.jpg"));
JButton song2 = new JButton("Paris", Song2Icon);
add(song2);
}
}
Explanation / Answer
add this code ClassLoader CLDR = this.getClass().getClassLoader(); InputStream soundName = CLDR.getResourceAsStream("yourDirectory/soundclip.wav"); AudioStream audioStream = new AudioStream(soundName); AudioPlayer.player.start(audioStream);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.