Getting started with Java is much simpler than I thought. So far, I have an understanding of the basic syntax structure of Java. But I know that in-depth study of any language requires time and accumulation of practice.
Applet is a code written in Java that can be run on the browser side. The obvious difference between it and an application lies in its execution method. Applications such as C programs start running from the main() main program, while Applets It’s complicated. I don’t know exactly how complicated it is, but I’ll figure it out gradually. An important property about Applet is that I can pass the value in Html as a parameter to Applet (get the parameter value through getParameter()). In this way, in order to produce different effects, we do not need to recompile the Java program, but just Just modify the parameter values of the HTML. Since the HTML code can also be dynamically generated, I can control the dynamic effects of the web page as I wish.
There are three main methods in the Applet life cycle: init, start , stop
init(): Responsible for the initialization of the Applet, this method is only executed once during the entire Applet life cycle. It is the same as the OnCreate() event in Delphi
start(): The system finishes calling init() After that, start() will be called automatically, and this method will be called every time the current window is reactivated, which is similar to the OnShow() event in Delphi.
stop(): This method is called after the user leaves the page where the Applet is located. It allows you to stop the work of some resources when the user does not pay attention to the Applet so as not to affect the system operating efficiency. And we do not need to artificially remove this method. This method is similar to the OnClose() event in Delphi.
The following is an Applet version of HelloWorld
File name: HelloWorld.java
import java.applet.Applet;
import java.awt.Graphics;
public class HelloWorld extends Applet
{
String title;
public void init (){
title="Hello World";
}
public void paint(Graphics g)
{
g.drawString(title,50,20);
}
}
We can see that in the program There is no main function, so how does it run? Since the Applet runs in the browser environment, we need to call it in the HTML file. The relevant tag that needs to be used is the
The above is the content of the memo (2) for beginners learning Java. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!