Java entry note 2

xiaoxiao2021-03-06  43

3. applet

3.1 Hello World Applet

Next, use the Applet to implement Hello World, let's learn the easiest applet program and how to use it.

1: Import java.awt.graphics;

3: public class helloworldapplet extends java.applet.applet {

4:

5: Public void Paint (Graphics g) {

6: G. DrawString ("Hello World!", 5, 25);

7:}

8:}

Applet Description:

(1) First applet is generally inherited from java.applet.applet;

(2) The Paint () method must be public, otherwise it cannot be called correctly by other programs;

(3) Since the PAINT method uses the Graphics class, Applet needs to reference Java.aw.

(4) Graphics, of course, if other classes can also realize the functionality of the Graphics class, it can also be referenced;

(5) There is no public static void main method in the applet, so it cannot be executed independently, and must be executed by a dedicated program (such as an excurs), the Applet program is called, and the PAINT method will be automatically executed.

Such an applet is completed, saved in the HelloWorldapplet.java file name, compiled using the normal compile command, Javac HelloWorldapplet.java, will generate a .class file after compiling, then introduce an applet applet introduced in the web page. Here we introduce this applet applet in HTML:

(6) This is a simple applet reference method. Use the tag applet in HTML to reference the applet, the code property specifies the applet's program name, and the applet introduced in this example is in the same directory in the same directory, so there is no path. Explanation, if you are not available to specify the path where the applet is located, the path representation is the same as the HTML;

(7) Other attributes Width Timely HEIGHT is the standard attribute of HTML

(8) However, the HTML is executed in the video, and the Applet applet is not necessarily executed. For specific reasons, I haven't found it yet. The operating system I use is XP. The browser is IE6.0, and Java virtual machine has been installed. It has been tested that there is a website with applets. If someone knows, please don't visit, tell me (email: 8280338@tzenet.com), everyone will progress.

There is another way to test the applet, which is used using the appletViewer command. If the above HTML file is called Hello.htm, then you can use:

AppletViewer Hello.htm

Check the Applet applet introduced in the HTML file, which ignores the HTML code and execute the Applet code directly. 3.2 Applet parameter usage

You can use the tag to pass parameters to the applet in the applet, such as:

......

When the applet is executed, these parameters will be incomed, and the value of these incoming parameters can be used in the init method, such as getParameter ("font"), the return value is always String type, so The corresponding type conversion needs to be required. If there is no corresponding incoming parameter, the getParameter () method returns a NULL value.

3.3 Applet Basic Method

There will be five basic methods from the Applet from java.applet.applets.

(1) Init () method: When the applet is loaded, the init method is first executed, which is equivalent to Main in the Java Application, which can only be executed once;

(2) START () method: The init method will automatically execute the start method. The START method can be repeated. If an applet is STOP, you can use the start method to reopen it;

(3) STOP () method: Stop execution applet, correspond to the start method

(4) DESTROY () method: corresponds to the init method;

(5) Paint () method: output the specified content, the Paint method will be repeated, and when the applet is initialized, the window moves when moving;

(6) Repaint () method: This method calls the Update method;

(7) Update () method: After emptying the contents of the screen, then call the Paint method to redraw; Paint, Repaint and Update methods are three main functions of the animation in the applet, generally call G.clipRect methods in the UPDATE method. Rule Paint area, so that the PAINT method only updates the contents of the specified area;

(8) The above methods can be overloaded at the time of the must, and it is not necessary to be invoked in our applet (of course, you can also display call);

3.4 Threads in Applet

It is best to use threads if you need to use a large number of resources that need to take a lot of resources, or so you need to take up a large number of resources. The following is a simple example of using threads in the applet:

Import java.awt.graphics;

Import java.util.date;

Public Class DigitalClock Extends Java.Applet.Applet

Implements runnable {

Date these; thread runner;

Public void start () {

IF (runner == null) {

Runner = new thread (this);

Runner.start ();

}

Public void stop () {if (runner! = null) {

Runner.stop ();

Runner = NULL;

}

Public void run () {

While (true) {

theDate = new date ();

Repaint ();

Try {thread.sleep (1000);

Catch (InterruptedExcect E) {}

}

Public void paint (graphics g) {

g.drawstring (thisdate.tostring (), 10, 50);

}

Description:

(1) To support the thread in Applet, it is generally necessary to implement the RunNable interface, so in the declaration of this Class, IMPLEments Runnable;

(2) The RUN () method is defined in the runable interface. This method is called when threaded start; so it is necessary to implement the method;

(3) To use threads, it is of course necessary to instantiate THREAD (Thread is a java.lang package): Thread runner = new thread (this), the initialization parameter this specified Applet object;

(4) Next, in the start method of the applet, call the runner.start () method to start creating a thread, running the RUN method after the thread is created;

(5) Implementing the true implementation in the RUN method, this example modifies the value of theDate, call the repaint method, the call triggered the execution of the PAINT method;

(6) End the running of the thread in the STOP method.

转载请注明原文地址:https://www.9cbs.com/read-55536.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.039, SQL: 9