Java Applet

How to Create a Java Applet

Project: Java Applet

Java is a programming language used to create web apps, mobile apps, and desktop apps. Developed and maintained by the nonprofit Java programming community, the java language is widely used in educational institutions and enterprises. It has also become essential in creating web applications and games. For this reason, learning java can have a significant impact on your career as a web developer or a programmer. Web developers who understand java frequently create apps for their websites using the language.

A java applet is a small program that runs within a browser window. All java applets are standalone applications that run on a computer’s RAM without taking up space on the hard drive. To run, an applet must be embedded into a web page or application using HTML5 or JavaServer Pages (JSP). Applets are interactively designed with input controls, output messages and animations to create an engaging user experience. The look and functionality of an applet can be entirely customized through code and graphics editors.

Guild: Java Applet

We discuss how to make an applet in Java with this tutorial, and the following steps can be adapted in other to achieve this:

1 – Create a java program in JCreator or NetBeans and name it sampleApplet.java. In the classname, reach a JApplet to have an applet library.

2 – Proceed to import the javax.swing.* and awt.* bundle because we will have the JLabel swing and the container.

				
					import javax.swing.*;
import java.awt.*;
				
			

3 – Create an init() capacity to input a cluster. The code below will be inserted into the init() work.

				
					public void init(){
		//create a container that will have the components to be placed
		Container con = getContentPane();
		// have the flowlayout as the layout manager to directly arrange the component
		con.setLayout(new FlowLayout());
		// have the label to have the text "Hello World"
		JLabel lbl = new JLabel("Hello World!");
		// then add the label into the component
		con.add(lbl);
	}
				
			

Remember you have to develop the program to have the .class file extension for viewing the applet.

4 – Navigate to create a new file
– Click on file type
– Select other folder
– Click on HTML Applet.
After completing the process, it will have the following code, insert and encode the file name of the developed java file in the Applet Code, the sampleApplet.class. The Below code can be followed:

				
					<html>
    <head>
    </head>
    <body bgcolor="000000">
        <center>
            <applet
                code	= "sampleApplet.class"
                width	= "500"
                height	= "300"
                >
            </applet>
        </center>
    </body>
</html>
				
			

5 – Proceed to run the program after building.

Complete Source Code: Java Applet

				
					import javax.swing.*;
import java.awt.*;
 
public class sampleApplet extends JApplet{
 
 	public void init(){
 
 		Container con = getContentPane();
 		con.setLayout(new FlowLayout());
 		JLabel lbl = new JLabel("Hello World!");
 		con.add(lbl);
 	}
 
}
				
			

The tutorial clearly illustrates how to create a Java Applet.

Don’t forget to share this post!

Leave a Comment

Your email address will not be published. Required fields are marked *

Related Article