Academic Java    Java Tutorial  >  Applets  >  ActionListener for Applet

ActionListener for Applet Example

Java ActionListener for Applet EXAMPLE output A button and text area have been added to the contentPane of this applet.

An ActionListener has been added to the button. A JTextArea is used for the applet's output.

Initially the Java version being used is output. When the button is pressed another message is output.
// ActionListener for Applet Example
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class ActionListenerAppletExample extends JApplet {

   JTextArea text = new JTextArea();

   @Override public void init() {
      JButton button = new JButton("Press Me");
      button.addActionListener( new ActionListener() {
         public void actionPerformed( ActionEvent e ) {
            text.append("Button Pressed!\n");
         }
      } );

      Container cp = getContentPane();
      cp.setLayout(new BorderLayout());

      cp.add(button,BorderLayout.SOUTH);
      cp.add(text,BorderLayout.CENTER);
      String version = System.getProperty("java.version");
      text.append("Java Version: " + version + "\n" );
   }

}