import java.applet.*; import java.awt.*; import java.awt.event.*; public class SoundBeepWithLabelAndButton extends Applet { public void init() { // Make the applet window narrow enough so that the button appears // *under* the label; note that the applet window can still be // resized (made larger) by the user, which can change the position // of the two objects; see other example programs to learn how to // specify the exact (absolute) position of applet window elements setSize(120, 60); // Construct a label Label info = new Label("Click the button!", Label.CENTER); info.setBackground(Color.red); // Construct a button Button beep = new Button("Beep"); beep.setForeground(Color.blue); // Add the label and the button to the applet layout; notice that no // explicit 'paint' method is needed, since components know how // to paint themselves; when an applet is repainted, it calls not // only its own 'paint' method, but also the 'paint' methods for // all of the components that it contains add(info); add(beep); // Specify that action events sent by the button should be handled // by a new 'BeepAction' object (defined below) beep.addActionListener(new BeepAction()); } } class BeepAction implements ActionListener { public void actionPerformed(ActionEvent event) { // Emit the native operating system audio beep Toolkit.getDefaultToolkit().beep(); } }