//******************************************************************** // JukeBoxControls.java Author: Lewis and Loftus // // Demonstrates the use of a combo box. //******************************************************************** import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing.*; import java.net.URL; public class JukeBoxControls extends JFrame { private JLabel titleLabel; private JComboBox musicCombo; private JButton stopButton, playButton; private JPanel appPanel, buttonPanel; private AudioClip[] music; private AudioClip current; //----------------------------------------------------------------- // Sets up the JukeBox GUI. //----------------------------------------------------------------- public JukeBoxControls() { super ("Java Juke Box"); setSize (300, 200); try { music = new AudioClip[7]; music[0] = null; // Corresponds to "Make a Selection..." music[1] = Applet.newAudioClip ( new URL("file", "localhost", "westernBeat.wav")); music[2] = Applet.newAudioClip ( new URL("file", "localhost", "classical.wav")); music[3] = Applet.newAudioClip ( new URL("file", "localhost", "jeopardy.au")); music[4] = Applet.newAudioClip ( new URL("file", "localhost", "newAgeRythm.wav")); music[5] = Applet.newAudioClip ( new URL("file", "localhost", "eightiesJam.wav")); music[6] = Applet.newAudioClip ( new URL("file", "localhost", "hitchcock.wav")); } catch (Exception exception) {} titleLabel = new JLabel ("Java Juke Box"); titleLabel.setAlignmentX (Component.CENTER_ALIGNMENT); // Create the list of strings for the combo box options. String[] musicNames = {"Make A Selection...", "Western Beat", "Classical Melody", "Jeopardy Theme", "New Age Rythm", "Eighties Jam", "Alfred Hitchcock's Theme"}; musicCombo = new JComboBox (musicNames); musicCombo.setAlignmentX (Component.CENTER_ALIGNMENT); playButton = new JButton ("Play", new ImageIcon ("play.gif")); playButton.setBackground (Color.white); stopButton = new JButton ("Stop", new ImageIcon ("stop.gif")); stopButton.setBackground (Color.white); buttonPanel = new JPanel(); buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS)); buttonPanel.add (playButton); buttonPanel.add (Box.createRigidArea (new Dimension(5,0))); buttonPanel.add (stopButton); buttonPanel.setBackground (Color.cyan); appPanel = new JPanel(); appPanel.setLayout (new BoxLayout(appPanel, BoxLayout.Y_AXIS)); appPanel.add (titleLabel); appPanel.add (Box.createRigidArea (new Dimension(0,5))); appPanel.add (musicCombo); appPanel.add (Box.createRigidArea (new Dimension(0,5))); appPanel.add (buttonPanel); appPanel.setBackground (Color.cyan); setContentPane (appPanel); musicCombo.addActionListener (new JukeBoxListener()); stopButton.addActionListener (new JukeBoxListener()); playButton.addActionListener (new JukeBoxListener()); current = null; } //***************************************************************** // An inner class to handle the various events of the juke box. //***************************************************************** private class JukeBoxListener implements ActionListener { //-------------------------------------------------------------- // Handles the play and stop buttons and the combo box. //-------------------------------------------------------------- public void actionPerformed (ActionEvent event) { if (current != null) // Stop current clip no matter what current.stop(); Object source = event.getSource(); if (source == playButton) { if (current != null) current.play(); } else if (source == musicCombo) current = music[musicCombo.getSelectedIndex()]; } } }