Home | Java Menu | Java Applets | Java Tips | E-mail Us
Java Applets
Date and time
Image animation
Window status message on mouse over images
Using the <PARAM> tag to collect user arguments
Graphical User Interface (GUI) - buttons, labels, checkboxes, and text areas
Multimedia using sound
Multimedia using image
Application and Applet combined
Running threads
Application and Applet combined

Running a Program as an Application and an Applet.

View Java Applet Source

Adding a main() method to an applet enables you to run the applet as an application or as an applet at the same time.

The following steps must be added in the
main() method:
 » Create a new frame object.
 » Create an instance of the applet.
 » Add the applet instance to the new frame.
 » Invoke the
init() method.

Here is a sample
main() method:

public static void main(String[] args)
{
    Frame f = new Frame();  //Create a new frame object

    DateTime2 dateTime = new DateTime2();  //Create an instance of the DateTime2 applet

    //Add the new instance into the frame
    f.setLayout(new BorderLayout());
    f.add("Center", dateTime);
    f.setSize(200,255);
    f.setVisible(true);

    //An "anonymous" inner class used to close the window
    f.addWindowListener( new WindowAdapter()
       {
           public void windowClosing(WindowEvent we)
           {
               System.exit(0);
           }
       }
    );

   dateTime.init();  //Call the applet's init method
}

To run the Java application with the Java interpreter in the JDK, you enter the command:
    
java DateTime2
Home | Java Menu | Java Applets | Java Tips | Legal
© 1999-2003 Complete e Solutions, Inc.