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"));
}
}