Applet merged with Application

Source: Internet
Author: User

Java is a technique for building distributed applications. It is the best tool for network programming. Extend the application of the Internet from communication tools to networks that can run mature applications.

There are two types of Java programs: applets and application. Simply put, applets are programs that embed Web documents, and application are all other types of programs.

In Java, applets and application are not limited in size and complexity, but because applets are primarily used for network communication, applets are generally smaller in size because of the limited speed of communication and the longer download time. For application, there is no such concern.

The technical difference between applets and application comes from the difference in their operating environment. The applet requires a large amount of information from the browser: the location and size of the browser client, the parameters of the embedded HTML document, the initialization process (init), the START process (start), the Stop process (stop), the termination process (destory), the drawing process (paint), and so on, Application is much simpler, and the only input from the outside world is command-line arguments.

For applets that must be java.applet.Applet, application must have a public method main (). Second, the main thread of the two is different, and the applet is initialized by Method Init (), while the application is started by method Main () to run the program. In general, Java applets and application are programmed in full compliance with the above principles, but we can use techniques to write programs that are applets and application. This gives us a better understanding of the internal structure of Java and enables the same program to run in different operating environments, and also to improve interest in Java research.

The routines are as follows:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Example extends Applet
implements ActionListener{ // #1
public static void main(String[] args){
Frame win=new Frame(?xample");
Example example=new Example();
win.add(?enter", example);
example.init(); // #2
win.setSize(600,360);
win.setVisible(true);
}
public void init(){ // #3
Button button;
Label label;
button=new Button(" OK "):
button.setBounds(280,200,100,20);
button.addActionListener(this);
label=new Label();
label.setBounds(260,100,200,20);
add(button);
add(label);
}
public void actionPerformed(ActionEvent e){
Object source=e.getSource();
if(source==button){
label.setText(" It is a example !");
}
}
}

In the program, you must let the program inherit the Java.applet.Applet class this is the necessary condition for the applet, in the corner, you need a raw cost class of an instance example, through this instance to invoke the Init () method, #3 is the init () method.

If you think of the program as an applet, it simply overrides the Init () method and the Actionperformed () method to respond to the button event. If as a application, the main () method begins, and Mr. The program is added to the window by an instance of the program itself, and then the init () method is invoked. Compile this program, you can be prompted to run as a application directly, and can be used as an applet into any browser.

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.