/* 
       Import all matching classes: 
       the standard Applet and Abstract Window
       Toolkit classes.
*/  
import java.applet.*;
import java.awt.*;
/*
      Declare class "clickcount" 
      as a subclass of class "Applet"
*/  
public class clickcount extends Applet
{ /* start class clickcount */  
  int count ;
  TextField f;
      /*
            This code builds the display 
            of the applet (Figure  37.3)
            It redefines "init"
      */  
  public void init ()
  { /* start init */  
    count = 0; 
       /*
             Create an instance of "Button" 
             and add it to the applet's
             window.
       */  
    add(new Button ("Click Here") );
    f = new TextField 
        ("   The button has not been clicked at all.");
    f.setEditable(false);
    add(f);  /* Associate f with the applet window. */  
  } /* stop init  */  
      /*
            This code redefines "action."  
            It determines the action performed
            when the user clicks on the button.
      */  
  public boolean action (Event e, Object arg) 
  { /* start action  */  
     if ( ( (Button) e.target).getLabel() == "Click Here")
     {/* start if  */  
        count += 1;
        f.setText 
        ("     The button has been clicked " + count + " times.");
     }/* stop if  */  
     return true;
  } /* stop action  */  
}/* stop class clickcount */  
     
 
 <HTML><HEAD><TITLE> Java Demo </TITLE> </HEAD><BODY> <H2><CENTER> <FONT SIZE=5>Java Demo</FONT> </CENTER></H2> <APPLET codebase="." code="clickcount.class" width=500 height=100></APPLET> </BODY></HTML>
import java.applet.*;
import java.net.*;
import java.awt.*;
public class buttons extends Applet 
{
   public void init() 
   {
      add(new Button("Ying"));
      add(new Button("Yang"));
   }
   public boolean action(Event e, Object arg) 
   {
      if (((Button) e.target).getLabel() == "Ying") 
      {
         try {getAppletContext().showDocument 
                 (new URL("http://www.apple.com"));}
         catch (Exception ex) 
         {  /* note: code to handle 
               the exception goes here */  }
      }
      else if (((Button) e.target).getLabel() == "Yang") 
           {
              try {getAppletContext().showDocument
                      (new URL("http://www.microsoft.com")); }
              catch (Exception ex) 
              { /* note: code to handle 
                   the exception goes here */   }
           }
      return true;
   } /* end public boolean action */  
} /* end public class buttons */  
     
<HTML> <HEAD> <TITLE> JavaScript counter </TITLE> </HEAD>
<! THE onLoad DIRECTIVE IN THE BODY TAG HERE MEANS THE
BROWSER WILL INVOKE FUNCTION incrementcount WHENEVER THE
DOCUMENT IS LOADED.  -> 
<BODY onLoad="return incrementcount(document.demo);">
<! THE JAVASCRIPT PART OF THE HTML DOCUMENT STARTS HERE
IT STARTS WITH A FUNCTION DECLARATION THEN IT CHOOSES A
MESSAGE TO DISPLAY, BASED ON THE VALUE OF n THEN IT DISPLAYS
THE MESSAGE -> 
<SCRIPT>  
var n;
n = -1;
function incrementcount (form)
{
  n = n + 1 ;
  if (n==0)
  {
    s = "  The button has not been clicked at all.  " ;
  }
  else
  {
    s = "  The button has been clicked " + n + " times." ;
  }
  form.elements["OutputArea"].value = s ;
}
</SCRIPT>
<! THE JAVASCRIPT PART OF THE HTML DOCUMENT ENDS HERE ->  
<CENTER>
<! HTML DIRECTIVE TO DISPLAY A FORM ->   
<FORM NAME="demo">
<! SPECIFY THE FUNCTION TO EXECUTE 
      WHEN THE BUTTON IS CLICKED ->   
<INPUT TYPE="BUTTON" NAME="ButtonClick" value="Click here "
    onClick="incrementcount(this.form);">
<! SPECIFY THE DISPLAY AREA ->   
<TEXTAREA NAME="OutputArea" ROWS=1 COLS=42>
</TEXTAREA> </FORM> </CENTER> </BODY> </HTML>