import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.applet.AudioClip; import java.net.URL; public class JukeBoxView extends JPanel { private JukeBoxController jukeBoxController; private SongComboBox musicCombo; class TitleLabel extends JLabel { TitleLabel() { super("Java Juke Box"); setAlignmentX (Component.CENTER_ALIGNMENT); } } class SongComboBox extends JComboBox { SongComboBox() { super(); setAlignmentX (Component.CENTER_ALIGNMENT); addActionListener (new ActionListener() { //-------------------------------------------------------------- // Stops playing the current selection (if any) and resets // the current selection to the one chosen. //-------------------------------------------------------------- public void actionPerformed (ActionEvent event) { jukeBoxController.setCurrent((String)(musicCombo.getItemAt(musicCombo.getSelectedIndex()))); } }); } } class SongButtonPanel extends JPanel { class PushButton extends JButton { PushButton(String name) { super(name); setBackground(Color.white); setMnemonic(name.charAt(0)); addActionListener (new ActionListener() { public void actionPerformed (ActionEvent event) { jukeBoxController.handleCommand(event.getActionCommand()); } }); } } // Set up the buttons SongButtonPanel(String[] commands) { super(); setLayout (new BoxLayout (this, BoxLayout.X_AXIS)); for (int i = 0; i < commands.length; i++) { add (new PushButton(commands[i])); add (Box.createRigidArea (new Dimension(5,0))); } setBackground (Color.cyan); } } //----------------------------------------------------------------- // Sets up the GUI for the juke box. //----------------------------------------------------------------- public JukeBoxView(JukeBoxController jbc, String[] commands) { jukeBoxController = jbc; musicCombo = new SongComboBox(); // Set up this panel setPreferredSize (new Dimension (300, 100)); setBackground (Color.cyan); setLayout (new BoxLayout (this, BoxLayout.Y_AXIS)); add (Box.createRigidArea (new Dimension(0,5))); add (new TitleLabel()); add (Box.createRigidArea (new Dimension(0,5))); add (musicCombo); add (Box.createRigidArea (new Dimension(0,5))); add (new SongButtonPanel(commands)); add (Box.createRigidArea (new Dimension(0,5))); } public int getItemCount() { return musicCombo.getItemCount(); } public String getSelectedItem() { return (String)(musicCombo.getSelectedItem()); } public void setSelectedIndex(int i) { musicCombo.setSelectedIndex(i); } public void addSong(String title) { musicCombo.addItem(title); } }