 |
|
 |
 |
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 |
|