12.3 Using Java in JavaScript
With JavaScript, you can use a small application (Applet), plugin, and a plurality of forms, hyperlinks, and images including forms, hyperlories, and images including forms, hyperlories.
JavaScript uses an array called Applets to access Java's Applet. The Applets array is an attribute of the browser object Document. Each of this array is arranged in the order in which they appear in the HTML source code, and the debate value starts at 0.
The Applet object in the corresponding JavaScript language can be represented in the form of document.applets [0], or you can use Document.blink to access, Blink is the name of this applet.
There is a special situation that cannot access Java properties through the network in JavaScript, that is, JavaScript can only call various common variables and properties in the local Java applet. Such a design is to protect JAVA's security.
In addition to accessing Java's small apps in JavaScript, you can use JavaScript to access any class of the Java package name space. The concept of class (Class) mentioned in the Java language is consistent with the object concept in JavaScript.
Tip: Various objects in Java are organized to categorically, and the classes are organized into packages, and the subclasses in the package are inherited from their base classes, and expand and change.
Classes in any Java can use a simple syntax corresponding to an object in JavaScript. For example: Var javasystem = java.lang.system;
The object JavaSystem in JavaScript corresponds to the java.lang.system object in the Java language, which is directly accessed in JavaScript to access various methods and properties in java.lang.system. For example, JavaSystem.err.Println can be used in JavaScript ("Java !!" from JavaScript); use Java's method to output a string.
In addition to the classes provided directly from Java, you can also create an instance of a Java class in JavaScript, as if this class is the same as the JavaScript itself. For example, you can create a Date class in Java in JavaScript: var mynewdate = new java.util.date (); this created MynewDate object can access all Java class Date data.
By using JavaScript's functionality directly accessing the Java class library, you can greatly extend the use range of JavaScript, so that it is no longer limited to small scope, and most of the functions that can be implemented in Java languages, such as network access And complex window response, etc.
Example: A small application import java.applet.applet; import java.awt.graphics; import java.awt.font;
Public class controljava extends applet {font f = new font ("Timeesroman", Font.Bold, 60); string text;
Public void init () {text = new string ("This is a Java's applet, which can be controlled in JavaScript.");
Public void settext {text = newText; repaint ();} public void Paint (graphics g) {g.setfont (f); g.drawstring (text, 15, 50);}}
This applet provides a public approach called setText (), which can set the text displayed by this applet, where this method is used in JavaScript to control the input of the Java applet.
Example: