Java is becoming more and more widely used in www because of its flexibility, and many web pages use Java to increase dynamic and interactive conversations with users. Java script and Java applets are most commonly used in www. Java script is fast to start, simple to write, easy to modify, and the combination of documents closely, but its function is slightly too simple; Java applets are powerful and flexible, but are written in a complex way and cannot be modified directly in the document. Therefore, if the combination of the two can be used to achieve good results.
Let me give you an example of how to use Java Script and Java applets in a Web page.
First of all, we write a Java applet, called HelloWorld1 Good (everyone to the HelloWorld must not be unfamiliar).
Helloworld1.java
import java.awt.*;
import java.applet.*;
public class HelloWorld1 extends Applet
{
public String text="Hello World!";//请注意此处的public
public void paint(Graphics g)
{g.drawString(text,20,20);}
}
Helloworld1.class will be generated after compiling (Don't tell me the program can't pass!) If compiling errors, check to see if the spelling is incorrect. )
Example.html
<body>
<script language="JavaScript">
<!--
function SetText()
{
document.app.text="世界你好!";
document.app.repaint();//也请注意此句.
}
//-->
</script>
A comprehensive example of Java script and Java applets <br>
<applet code="HelloWorld1.class" width="100" height="28" name="app">
</applet>
<form>
<input type="button" value="请点击这里" onclick="SetText()">
</form>
</body>
Opening example.html with the browser will appear with a "Hello world!" Java applet and a button "please click here". We'll click the button and you'll find "Hello world!" into "The World Hello". This shows that Java script has successfully changed the content of text in HelloWorld1.