Applets Load Java applications

Source: Internet
Author: User

The question of how to write a small application (applet) and embed it in a Web page is discussed in many books on Java. Instead of repeating this type of problem here, we discuss how to use a small application as a widget and allow the application that you write to load and run the Java applet normally.

What is a small application (applet)

Applets (small applications) are often considered to be security-protected applications that embed Web pages. It should be said that such a definition is not very precise. Let's take a look at the implications of small applications first.

Figure 1 This figure shows a scenario where "Simpleapplet" runs as a standalone application

The base class for a small application is the Java.applet.Applet class, which expands from the Java.awt.Panel class, so it can be said that a small application is a panel. And the Java.awt.Panel class extends from the Java.awt.Container class, so you can also think of small applications as containers (Container). Look down and you'll find Java.awt.Container

Class extends from the Java.awt.Component class, so it can be said that small applications (applets) are artifacts (Component), which means that small applications are capable of handling various events and can be added to various containers.

Make a small application work as a component

In the above discussion, we have made it clear that small application applets are artifacts (Container), which means that small application applets can be embedded into containers in the appropriate form.

Figure 2 Scenario where a small application is loaded into the application runtime with another class

1, with the main () method to load, run the small application

To embed a small application in another program, you can use it as a normal application, and the key to this is to complete the instantiation of the small application in the main () method, call the Init () and start () method for the small application, and create a new framework for the small application. and incorporate small applications into them. The list of source programs is as follows:

Simpleapplet.java

import java.applet.*;

import java.awt.*;


public class SimpleApplet extends Applet{

public static void main(String[] args){

Frame f=new Frame("A frame!");

SimpleApplet h=new SimpleApplet();

h.init();

h.start();

f.add("Center",h);

f.pack();

f.show();

}

public void init(){

add(new Label("I am a Component"));

}

}

Related Article

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.