A browser makes a request and downloads
a copy of the binary file. /*
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 at right)
It redefines "init"
*/
public void init ()
{ /* start class 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. */
} /* end class 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 class action */
if ( ( (Button) e.target).getLabel() == "Click Here")
{ /* start if */
count += 1;
f.setText
(" The button has been clicked " + count + " times.");
} /* end if */
return true;
} /* end class action */
} /* end 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>
The applet uses the HTTP client to retrieve documents and the HTML
interpreter to display them. import java.applet.*;
import java.net.*;
import java.awt.*;
public class buttons extends Applet
{
public void init()
{
add(new Button("Yin"));
add(new Button("Yang"));
}
public boolean action(Event e, Object arg)
{
if (((Button) e.target).getLabel() == "Yin")
{
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. THE EFFECT OF THIS IS TO INITIALIZE
THE DIALOG PANEL.->
<BODY onLoad="return incrementcount(document.demo);">
<! THE JAVASCRIPT PART OF THE HTML DOCUMENT STARTS HERE.
THE JavaScript INTERPRETER TRANSLATES THIS SECTION.
IT CONTAINS THE DEFINITION OF A FUNCTION. THE FUNCTION CHOOSES A
MESSAGE TO DISPLAY BASED ON THE VALUE OF A VARIABLE n, AND
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 ->
<BR><BR>
<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>