0% found this document useful (0 votes)
16 views3 pages

Applet

The document outlines the steps to execute a Java applet in Eclipse, including installing Java 8, creating a Java project and class, writing applet code, and running the applet via an HTML file or Applet Viewer. It also describes how to install WindowBuilder in Eclipse and create a new Swing application, specifically a calculator, using the GUI designer. The guide provides detailed instructions for each step to ensure successful execution and design of the applications.

Uploaded by

p38981094
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views3 pages

Applet

The document outlines the steps to execute a Java applet in Eclipse, including installing Java 8, creating a Java project and class, writing applet code, and running the applet via an HTML file or Applet Viewer. It also describes how to install WindowBuilder in Eclipse and create a new Swing application, specifically a calculator, using the GUI designer. The guide provides detailed instructions for each step to ensure successful execution and design of the applications.

Uploaded by

p38981094
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Steps to Execute the Applet in Eclipse

Step 1: Install Java 8 or Earlier


Ensure that Java 8 (or earlier) is installed because applets are removed in later
versions.
1. Open Eclipse.
2. Go to Window → Preferences → Java → Installed JREs.
3. Select a JDK version 8 or lower.

Step 2: Create a New Java Project

1. Open Eclipse.
2. Click File → New → Java Project.
3. Enter AppletAddition as the project name.
4. Click Finish

Step 3: Create a New Java Class


1. Right-click on src → New → Class.
2. Name it AddNumbersApplet
3. Uncheck the "public static void main(String[] args)" option (Applets don't use
main()).
4. Click Finish.

Step 4: Write the Applet Code


import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/* <applet code="AddNumbersApplet" width=300 height=200></applet> */
public class AddNumbersApplet extends Applet implements ActionListener {
TextField num1, num2, result;
Button addButton;
public void init() {
setLayout(new FlowLayout());
Label l1 = new Label("Number 1:");
Label l2 = new Label("Number 2:");
Label l3 = new Label("Result:");
num1 = new TextField(10);
num2 = new TextField(10);
result = new TextField(10);
result.setEditable(false);

addButton = new Button("Add");


addButton.addActionListener(this);

add(l1);
add(num1);
add(l2);
add(num2);
add(addButton);
add(l3);
add(result);
}

public void actionPerformed(ActionEvent e) {


try {
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
int sum = n1 + n2;
result.setText(String.valueOf(sum));
} catch (NumberFormatException ex) {
result.setText("Invalid Input");
}
}
}

Step 5: Create an HTML File for Running the Applet


Eclipse does not directly support running applets. You need an HTML file.

1. Right-click src → New → File.


2. Name it AddNumbersApplet.html.
Paste the following code:

html
CopyEdit
<html>
<body>
<applet code="AddNumbersApplet.class" width="300" height="200"></applet>
</body></html>

Step 6: Run the Applet in Eclipse

1. Click Run → Run Configurations.


2. Select Java Applet.
3. Click New Launch Configuration.
4. Under Main Class, type AddNumbersApplet.
5.Click Run.

Alternatively, you can use Applet Viewer:


Open Command Prompt (cmd).
 Navigate to the compiled class file directory:
cd path\to\your\project\bin
 Run the applet using:
appletviewer AddNumbersApplet.html
How to Find WindowBuilder in Eclipse & Create a New Swing
Application (Calculator Example)
WindowBuilder is a powerful GUI designer for Java Swing, SWT, and AWT in
Eclipse. If you don't see it in Eclipse, follow these steps to install and use it.

Step 1: Install WindowBuilder (If Not Installed)


 Open Eclipse.
 Click Help → Eclipse Marketplace.
 In the Find box, type "WindowBuilder".
 Select WindowBuilder - Swing/SWT Designer.
 Click Install and follow the prompts.
 Restart Eclipse after installation.

Step 2: Create a New Swing Application


 Open Eclipse.
 Click File → New → Other.
 Search for "WindowBuilder".
 Select Swing Designer → JFrame.
 Click Next.
 Name the class (e.g., CalculatorSwing).
 Click Finish.

Step 3: Design the Calculator UI


 Switch to Design View (if not already open).
 Drag and drop components from the Palette:
 JTextField (for input/output).
 JButton (for numbers and operations).
 JPanel (for layout).
 Right-click on a button → Add Event Handler → ActionListener to handle
button clicks.

Design a calculator:
https://youtu.be/mZVD5OssHQM?si=NvSCFpTq9jn2yGf1

You might also like