How to Make an Applet With Eclipse for Java

Techwalla may earn compensation through affiliate links in this story. Learn more about our affiliate and product review process here.

Eclipse is a powerful free and open-source Integrated Development Environment (IDE) originally designed for Java developers in 2001, which has since expanded to include projects covering multiple languages and platforms. Among other tasks, it is an ideal environment for producing applets, special Java programs designed to be embedded within Web pages to provide interactive Web content.

Advertisement

Step 1

Create a new project by clicking "File" and "New Project." Name it "Applet Tutorial."

Video of the Day

Step 2

Click the "Workbench" button to open your newly created project.

Advertisement

Step 3

Create a new class by clicking "File" and "New Class." The name is "MyApplet." Next to "Superclass," click "Browse." Type "java.applet.Applet" and click "OK." Click the "Generate Constructors from superclass" box.

Advertisement

Step 4

Paste the following class definition within the source code for MyApplet.java:

public class MyApplet extends Applet implements ActionListener { Label label1 = new Label("Hello World."); Button button1 = new Button("OK");
}

Advertisement

Step 5

Add import statements for all the objects used in the above code. To do this, go through "ActionListener," "Label" and "Button," and, while the cursor is within that word, press "Ctrl-1" (or "Cmd-1" on Mac). Select "Import" from the list to allow Eclipse to perform the imports automatically. Finally, press "Ctrl-1" over "MyApplet" and choose "Add Unimplemented Methods." This will create an "actionPerformed" method to react to the user clicking the button.

Advertisement

Step 6

Define the user interface elements just below the class declaration:

public class MyApplet extends Applet implements ActionListener {

Step 7

Paste the following code within the constructor:

Step 8

Paste the following code within the "actionPerformed" method that was automatically generated in Step 5:

Video of the Day

Advertisement

Advertisement

references & resources