The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
September 2000



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


Add a main () method

Posted by Pedro B. Morales on September 09, 2000 at 8:53 PM


To be able to convert an applet into a Java application, you must add a main () method in the same class that is the applet.
This main method must create a Frame (or JFrame) object, create a new instance of the applet, add this instance to the frame, and then make it visible. Here's a sample main method, for an applet class named Applet1.

public static void main (String [] args)
Applet1 app = new Applet1 ();
app.init ();
Frame frame = new Frame ();
frame.setSize (applet_width, applet_height);
frame.add (app);

/* I don't know how to call paint without the graphics object, maybe you can try app1.paint (app1.getGraphics ()); or app1.repaint(); I would suggest repaint () in order to avoid calling getGraphics (), which may be null until the applet is fully loaded.

frame.setVisible (true);
frame.requestFocus (); // make it active program

This main method will allow you to wrap all your classes into a JAR file, and then distribute that.



Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us