devxlogo

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();    frame.show();}

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

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

See also  How Engineering Leaders Spot Weak Proposals

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.