Open In App

How to Use Swing Applet in Java?

In this article, we will be using the swing Applet or JApplet in Java. Here, we will make a simple multiplication application that will multiply the two input numbers.

Approach to Using Swing Applet in Java

Steps for Creating and Running the Swing Applet

The folder view of the project is shown in the below image.



Step 1: First, create the file as SApplet.java and enter the code into it.



Step 2: Once the code is been inserted then we need to create the SApplet.html file, through which we will display the output.




<!DOCTYPE html>
<html>
<head>
    <title>Java Applet Example</title>
</head>
<body>
    <applet code="SApplet.class" width=220 height=90>
        No Support
    </applet>
</body>
</html>

Step 3: Then we need to compile the Java code using the below command:

javac SApplet.java

Step 4: Now finally we need to run the application using the below power:

appletviewer SApplet.html

Program using SApplet

Implementation of the SApplet program is mentioned below:




// Java Program for Using Swing Applet in Java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
  
// Defining a class named SApplet that extends JApplet and
// implements the ActionListener interface
public class SApplet extends JApplet implements ActionListener {
    JTextField num1, num2, res;
    JButton resBtn;
  
    // This is Initialization method for the applet
    public void init()
    {
        // Creating three text fields for number input and
        // one button for calculation result
        num1 = new JTextField(5);
        num2 = new JTextField(5);
        res = new JTextField(10);
  
        // Make the result field non-editable
        res.setEditable(false);
        resBtn = new JButton("Multiply");
  
        // Register this applet as the action
        // listener for the button
        resBtn.addActionListener(this);
  
        // Creating labels for the input fields and result
        JLabel l1 = new JLabel("Enter Number 1:");
        JLabel l2 = new JLabel("Enter Number 2:");
        JLabel l3 = new JLabel("Result:");
  
        l1.setForeground(Color.black);
        l2.setForeground(Color.black);
        l3.setForeground(Color.black);
  
        // Creating a JPanel to arrange
        // components in a grid layout
        JPanel pan = new JPanel();
        pan.setLayout(new GridLayout(4, 2));
  
        // Adding labels, input fields, button, and empty
        // label for spacing
        pan.add(l1);
        pan.add(num1);
        pan.add(l2);
        pan.add(num2);
  
        // Set button background color
        resBtn.setBackground(Color.orange);
        pan.add(resBtn);
  
        // Empty label for spacing
        pan.add(new JLabel());
        pan.add(l3);
        pan.add(res);
  
        // Set text color Customize the fonts of various
        // components
        pan.setForeground(Color.black);
        Font lfont = l1.getFont();
  
        // Creating a new font with size 20
        Font inFont = lfont.deriveFont(Font.PLAIN, 20);
  
        l1.setFont(inFont);
        l2.setFont(inFont);
        l3.setFont(inFont);
  
        num1.setFont(inFont);
        num2.setFont(inFont);
  
        res.setFont(inFont);
        resBtn.setFont(inFont);
  
        // Set the layout of the applet to BorderLayout and
        // add the JPanel to the center
        setLayout(new BorderLayout());
        add(pan, BorderLayout.CENTER);
    }
  
    // ActionListener method to handle button clicks
    public void actionPerformed(ActionEvent e){
        
        // Check if the event source is the Multiply button
        if (e.getSource() == resBtn) {
  
            String str1 = num1.getText();
            String str2 = num2.getText();
  
            try {
                int num1 = Integer.parseInt(str1);
                int num2 = Integer.parseInt(str2);
                int result = num1 * num2;
  
                // Display the result in result text field
                res.setText(Integer.toString(result));
            }
            catch (NumberFormatException ex) {
  
                // Display an error message for invalid input
                res.setText("Wrong Input");
            }
        }
    }
}

Output for the Program:

Explanation of the Program


Article Tags :