Tutorial: How to Create a Java Applet with Source Code
Today in Java, I will show you how to make an applet in Java.
An applet is a little application that is seen on the Web utilizing java.
In this way, presently how about we start this instructional exercise!
1. Open JCreator or NetBeans and make a java program with a record name of sampleApplet.java. In the classname, stretch out a JApplet to have an applet library.
2. Import javax.swing.* and awt.* bundle since we will going to have the JLabel in swing and furthermore the Container.
import javax.swing.*; import java.awt.*;
3. Make an init() capacity to instate a cluster. At that point have this code inside 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); }
Note: You have to build your program to have the .class file extension for viewing of the applet.
4. Now, create a new file. Click File Type, choose Other Folder and click HTML Applet.
After creating it, it will have the given code. Then in the Applet Code, put and encode your file name of the created java file, the sampleApplet.class. Follow this code below:
<html> <head> </head> <body bgcolor="000000"> <center> <applet code = "sampleApplet.class" width = "500" height = "300" > </applet> </center> </body> </html>
5. Build and Run the program.
Complete Source Code of sampleApplet.java
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); } }
This tutorial is on How to Create a Java Applet. Download the Source Code Below.