Skills: JavaScript and Javaapplet integrated application

Source: Internet
Author: User
Tags functions modify variables variable
Javascript| Tips

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.

Here are some examples of how to use Java Script and Java applets in a Web page.

A comprehensive application of Java script and Java applets (i.)

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 applets {public String text= "Hello world!"; /Please note that public public void paint (Graphics g) {g.drawstring (text,20,20)} is here
       
        

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

       
        
         
        

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.

You may ask why you should add public to the text before the Helloworld1.java.

and the line in Java Script: "Document.app.repaint ();" What is the meaning??

That is true:

As in C + +, the object's properties are private in Java, and for Java script to access objects in the Java applet, it must be set to public (including functions, variables, and classes).

Document.app.text= "The World Hello!" Changes the value of text only, so that it is reflected on the browser, you must let HelloWorld1 refresh its display, so call the repaint () function in HelloWorld1. You may have to ask again: I did not see the repaint () function in the Helloworld1.java? HelloWorld1 is a subclass of applets, and we call repaint () actually call the repaint () function in the Class applet. Of course, it is not recommended to directly modify the variables in the applet externally, a more secure approach is to provide the public function to read and change the internal variables.

A comprehensive application of Java script and Java applets (II.)

Last time we introduced the method of using Java script to change the variables in the Java applet (it should be strictly to change the string class, but you can simply think of the string class as a variable); we say change Java directly in Java Script Variables in the applet are undesirable (at least not recommended). Because the Java applet itself does not know that the variable is changed, this poses a hidden hazard to the applet (remember we had to call the repaint () function to refresh the display?). A more positive approach is to provide the public function in the Java applet to support access to internal objects. Here's an improvement on Helloworld1.java and example.html, which describes how to invoke the Java applet's functions in Java script:

Given that browsers often use cache for Java applets with the same name, we name the program Helloworld2.java

Helloworld2.java

       
        
         
        Import java.awt.*; Import java.applet.*; The public class HelloWorld2 extends applet//main class name must be the same as the filename {String text= "Hello world!"; /Remove the public property of the text public void paint (Graphics g) {g.drawstring (text,20,20);}//This adds a public function public void setstring ( String newstring) {text=newstring; repaint ()//Automatic call to repaint () function}}
       
        

Generate Helloworld2.class after compiling;

Example2.html

       
        
         
        

Also open example2.html Click button and you will find "Hello world!" Became "Hello to the world!", stating that the calling function succeeded



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.