Make Your Program Both an Applet and an Application

Make Your Program Both an Applet and an Application

Sometimes it’s convenient to have your program be runnable both as an applet and as an application. One simple way of accomplishing this derives from the fact that any class can have a main method. If you’ve written your program as an applet, just add a main method, in which you instantiate the applet class and display it in a frame. For example:

public class MyApplet extends JApplet {  ...  public static void main(String[] args) {    Component applet = new MyApplet();    JFrame frame = new JFrame("My applet, as application");    frame.getContentPane().add(applet);    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    frame.pack();;}

Of course, depending on what components your applet displays, you may want to set the size explicitly rather than packing.

See also  How to Create and Deploy QR Codes Online: A Comprehensive Guide

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist