Hi purplepit-ga,
I will provide this answer of how to compile and run a Java applet in
terms of the applet that I previously created for you:
COMPILING:
1. First of all, bring the code that was provided into a file called
"But2.java". This is important because the name of the file (excluding
the .java) must be the same as the class being defined in the file.
2. Now we need to compile the file from the command line as follows:
> javac But2.java
3. This will produce a compiled file called "But2.class"
CREATING HTML WRAPPER
Since an applet is designed to be displayed in a browser, we need to
create an HTML file where we reference our compiled applet. It is
important to understand that browsers are not capable of loading
.class files unless they are embedded in an html file. The code for
this file is as follows:
----------Begin HTML Code---------------
<html>
<head>
<title>Welcome to the Button Applet</title>
</head>
<APPLET CODE = "But2.class" WIDTH = 500 HEIGHT = 500 >
</APPLET>
</html>
----------End HTML Code---------------
You can take the code above and save it in an HTML file (I named mine
But2.html just to be consistent). The most critical part of this file
is the APPLET tag, which references your compiled applet and also
defines the dimensions of the applet with the HTML page.
You should now have your But2.class and But2.html files stored in the
same folder.
RUNNING YOUR APPLET
At this point your applet can be run from a normal browser (such as
Internet Explorer or Netscape) by just opening the html file in the
browser, but I like to use the browser built into the Java Runtime
Environment you can invoke it from the command line as follows:
> appletviewer But2.html
This will bring up your applet the same way it would in any other
browser, but ensures that all necessary Java components are installed.
Thats all there is to it if you have any problems following these
instructions do post a clarification and I will get back to you
promptly.
Cheers!
Answerguru-ga |