/* * ============================================================== * DemoActionEvent.java : Action event example with one button * that demonstrates sources, events, and their listeners. * * Adapted from : Pantham S., Pure JFC Swing, 1999. * Modified by Mark Austin March, 2001 * ============================================================== */ import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; import java.net.URL; // import javax.swing.SwingUtilities; public class DemoActionEvent extends JApplet { Container container; JLabel label; URL codeBase; // URL for applet oodebase ... URL xmasURL; // URL for xmas image .. Icon xmasBells; public void init() { // 1. Get the handle on the applet's content pane. container = this.getContentPane(); // 2. Construct URL for image.... codeBase = getCodeBase(); try { xmasURL = new URL( codeBase, "bell.gif"); } catch ( java.net.MalformedURLException e ) { System.out.println("Badly specified URL!!"); } // 3. Download and save image icon .... xmasBells = new ImageIcon( xmasURL ); // 4. Add bell icon to swing label, then add label to applet. label = new JLabel( xmasBells ); container.add(label); // 5. Create a source (button) for the action event. JButton source = new JButton("Ring bell three times!"); container.add(source, BorderLayout.SOUTH ); // 6. Register the action listener with the source. source.addActionListener( new ButtonListener() ); } // 7. Define the listener (inner) class. class ButtonListener implements ActionListener { // 8. Interface method that has been implemented. // Ring the bell five times... public void actionPerformed(ActionEvent ae) { int i=0; while (i < 3) { Toolkit.getDefaultToolkit().beep(); try { Thread.currentThread().sleep(1000); } catch(InterruptedException ie) { System.out.println("Sleep Interrupted"); } i++; } } } }