How to Make an Applet With Eclipse for Java

By Kevin Walker

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.

Step 1

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

Step 2

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

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.

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"); }

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.

Step 6

Define the user interface elements just below the class declaration: public class MyApplet extends Applet implements ActionListener { Label label1 = new Label("Hello World."); Button button1 = new Button("OK");

Step 7

Paste the following code within the constructor: public MyApplet() throws HeadlessException { this.setLayout(new FlowLayout()); this.add(label1); this.add(button1); button1.addActionListener(this); }

Step 8

Paste the following code within the "actionPerformed" method that was automatically generated in Step 5: public void actionPerformed(ActionEvent e) { label1.setText("Button clicked."); }