Getting started with Java Applet
YY435
Pacific Network Academy
first day
Summary: [Development of Javapplets] [Compile and View Results] [JavaApplet] [JavaApplet] [related to C language]
Preparation of a first example: 1. Have a Java compiler, can be Sun's JDK compiler, or Microsoft's VJ , or some compilers developed by other companies, but the best Select VJ because it is the best Java compiler now.
2. A text write software, the general machine has, the notepad can be. If it is VJ , you can save this step. If it is JDK, it is best to be one.
3. Browser, can be Netscape, of course, Microsoft IE, other browsers, I don't know if I support Java, it is best to prepare these two in these two, I believe it is not difficult!
Two hands-written first Java Applet program: HelloWorld.java, you can use notepad. (Say with VJ)
The source code code is as follows: (the result is displayed on the right)
Import java.applet. *;
Import java.awt. *;
Public Class HelloWorld Extends Applet
{
Public void Paint (Graphics G)
{
g.drawstring ("Hello, Java World!", 2, 20);
}
}
(This example is very short. But you can explain a lot of questions, huh, huh, to explain it in detail later)
After entering this example, you must remember to save it with HelloWorld.java.
Three JDK compiler compile: Enter the following command in the command line of DOS:
C: /javajdk/javac.exe helloWorld.java
The front C: / Javajdk / is the name of the catalog installed, JavaC.exe is the name of the compiler, and helloworld.java is the name of this program you just prepared. If the compilation is unsuccessful, it is your source code code. If you have a problem, please check it carefully, compile it, and then generate a helloworld.class file. At this time, things are basically completed. Next is to put this generated .class file into the web page, build a web file, as follows:
body> html>
If you don't have to say it, you don't have to say it. Double-click this file, you can see this effect that has just been compiled, in the browser. Please see the front.
Four VJ compiled and developed the development environment
After starting the VJ development environment, it will have a new window, as follows (Figure 1):
I will open it, this time, VJ has helped you create a project file, VJ development, any program, it is made in a project, although your program is really small. In its Project Explorer, you can see two files that have been given to you, as shown in Figure 2: Because this program is too small, remove it, the applet1.java (right click, you can choose to delete), Create a new class yourself, click on the Add Class option in the "Project" menu, as shown in Figure 3:
Enter: HelloWorld.java in the class name. Entering the car, please press the source code I have given in the new class to add it complete, then, then open the "page1.htm" file in Figure 2, and take it ahead of me. Once you have changed, you can. In this VJ , you can also watch the results directly. If you press the F5 button, you can also watch in a real environment. These are here, if you want to know more about the VJ development environment, then you need to find a book, I can't do it, huh, huh!
Fifth pair of detailed explanations of the first example (explained "//" to marke it) Import java.applet. *;
/ / To quote Java's Applet class, of course you need to put it first! Equivalent to the #inlcude in the C language.
Import java.awt. *; // This is an input and output class, which is used to use the DrawString output function in the program, which is in this class library.
// These class libraries introduced earlier, others have helped us do well, don't use white, of course, you can also introduce your own class!
Public Class HelloWorld Extends Applet
// This is the formal entry of the Java Applet program, and the computer starts from here to perform your settings. Note: public is that the Applet is a public class, and the Class is a class, and the public class represents this is a public class. Then, the name of your program, that is, the name of the file must be the same as the class name of the program's cutout, otherwise compiling // is not. If you don't believe it, you can try it. Extends is the keyword of Java, just like the keyword in the C language, here // means it is an extension of the class library applet. Extends itself is the meaning of expansion.
{
Public void Paint (Graphics G)
// void is empty value, that is, this function does not return value, Paint is the default output function, there is a graphics type parameter
{g.drawstring ("Hello, Java World!", 2, 20);
// This is the output statement that is often used in Java. DrawString functions require three output variables, the first is a string, to use // quoted, second, three is the output position, respectively The value of the X, Y axis is worth noting that its Y-axis is positive direction.
}
The relationship between Java Applet and Java and C language Java applet is a subset of the JVAV language. It is a class, so it is full of Java, while JavaScript is not Java's subset, Java Applet and Java The syntax rules are exactly the same. Learning Java Applet is a must-part of learning Java. The Java language can be said that it is basically developed from the C language. Most of Java is inherited from C or C , but it has a write improvement on C or C , deleting many C or C restrictions on the machine. It is the biggest improvement in C or C or the pointer in C or C , which is definitely a good thing for users who are afraid of pointers. Of course, this also canceled the flexibility of C or C grammar Sex, this is good or bad, or wait for everyone to evaluate it. In short, all readers who have learned C or C will learn Java will be very fast. Java's grammar is just a view, focus on the difference between Java and C or C , and Java's new features.