5 Easy Steps to Convert a Java Application to an Applet

This assumes, of course, that you have a fairly basic application with nothing terribly fancy in the GUI. It also assumes that you have created your application as a single file extending the JFrame class as I demonstrated in class.

  1. Add import java.applet.*; to your import statements.
  2. Make your GUI class extend JApplet rather than JFrame.
  3. Remove all calls to JFrame methods, specifically the methods
    • setSize()
    • setTitle()
    • setDefaultCloseOperation()
    • setVisible
  4. Change your constructor heading to public void init ().
  5. Remove your main() method entirely. Yes, really.

Now you have an applet, which you can run either in the applet viewer component of your favorite IDE, or you can copy the ridiculously barebones HTML code given below into a text file with an .html file extension on the name to run it in an actual web browser. Notice that the HTML file does, in fact, use the deprecated <applet> tag because of cross-browser compatibility issues. If you want to know more about this, this page at Oracle’s web site will tell you more than you want to know.

<html>
<head>
<title>Example Java Applet</title>
</head>
<body>
<applet code = "ExampleGUIApplet.class" width = 300 height = 400 />
</applet>
</body>
</html>

Print Friendly, PDF & Email

Comments are closed