Download WRITING A JAVA APPLET

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

URL redirection wikipedia , lookup

Transcript
WRITING A JAVA APPLET
A Java applet is an object of superclass java.applet.Applet.
The life cycle of an applet moves through these stages:
It initializes
It starts executing
It stops executing
It is destroyed
These life cycle stages are triggered by the web browser in response to its user:
When the user opens the web page referencing the applet, the applet initializes and starts
executing.
When the user moves to another web page, or exits the browser, the applet stops and is
destroyed.
When the user returns to the web page, a new applet object is initialized and started.
When the user refreshes the web page, the current applet object is stopped and destroyed
and a new object is initialized and started.
For each stage, class Applet declares a corresponding method that the JVM calls when the
applet reaches that stage. Here they are:
java.applet.Applet Life Cycle Methods
Method
Called by the JVM when…
void init( )
The applet initializes.
void start( )
The applet starts executing.
void stop( )
The applet stops executing.
void destroy( )
The applet is destroyed.
Writing a Java Applet
Page 1
To write an applet, declare a subclass of Applet and morph whichever method gives the
behavior you want.
Example
To see the applet stages in action, copy this applet to jGRASP and compile and execute it there.
You do not need an associated HTML file to run applets in jGRASP. Observe when the methods
are called during the applet's life cycle.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.applet.Applet;
import static javax.swing.JOptionPane.*;
public class AppletLifeCycle extends Applet
{
public String name; // shared by methods
public void init( )
{
name = showInputDialog( "What's your name?" );
}
public void start( )
{
showMessageDialog( null, "Hello, " + name );
}
public void stop( )
{
showMessageDialog( null, "Goodbye, " + name );
}
public void destroy( )
{
showMessageDialog( null, "ARRRRGGGGG!" );
}
}
Writing a Java Applet
Page 2
An applet doesn't have System.in and System.out available to it like an application does
but you can do input and output from and to JOptionPane dialog boxes.
With this in mind, you can generally rewrite any Java application as an applet. The exception is
an application that stores data onto the host computer; this can only be done by an applet under
very strict conditions.
Example
Write a Java applet that converts U. S. dollars to Japanese yen. For example, $10.00 = ¥931.10.
import java.applet.Applet;
import static javax.swing.JOptionPane.*;
import java.text.DecimalFormat;
public class DollarToYenApplet extends Applet
{
// applet data
final double YEN_PER_DOLLAR = 76.56; // $1 = ¥76.56
DecimalFormat dfus = new DecimalFormat( "$#,###.00" );
DecimalFormat dfjp = new DecimalFormat( "\u00A5#,###.00" );
public void init( )
{
String input, output;
double usd, jpy;
input = showInputDialog( "Enter
usd = Double.parseDouble( input
jpy = usd * YEN_PER_DOLLAR;
output = dfus.format( usd ) + "
showMessageDialog( null, output
}
U.S. $ " );
);
= " + dfjp.format( jpy );
);
}
Writing a Java Applet
Page 3
Exercises
Use jGRASP to enter, save and compile the AppletLifeCycle applet. Answer the questions
below.
1.
Click the Run button. What happens? Why?
2.
Close the message dialog. Minimize the applet window. What happens? Why?
3.
Close the message dialog. Restore the applet window. What happens? Why?
4.
Close the message dialog. Close the applet window. What happens? Why?
5.
Use jGRASP to enter, save and compile the DollarToYenApplet applet. Run the
applet. What is the output for an input of 100 dollars?
6.
Write an HTML file that is appropriate for running the DollarToYenApplet applet.
Load the HTML file into Internet Explorer. What is the output for an input of 150 dollars?
What happens when you click the browser's reload button? Why?
Writing a Java Applet
Page 4