//file: NoisyButton.java
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class NoisyButton extends JFrame {
public NoisyButton(final AudioClip sound) {
// set up the frame
setTitle("NoisyButton");
addWindowListener(new WindowAdapter( ) {
public void windowClosing(WindowEvent e) { System.exit(0); }
});
setSize(200, 200);
setLocation(100, 100);
// set up the button
JButton button = new JButton("Woof!");
button.addActionListener(new ActionListener( ) {
public void actionPerformed(ActionEvent e) { sound.play( ); }
});
getContentPane( ).setBackground(Color.pink);
getContentPane().setLayout(new GridBagLayout( ));
getContentPane( ).add(button);
setVisible(true);
}
public static void main(String[] args) throws Exception {
java.io.File file = new java.io.File("bark.aiff");
AudioClip sound = Applet.newAudioClip(file.toURL( ));
new NoisyButton(sound);
}
}
|