Tuesday, November 4, 2008

Window2Applet

import java.awt.*;
import java.applet.*;
import java.awt.event.*;


// StandaloneApplet is an applet that runs either as
// an applet or a standalone application. To run
// standalone, it provides a main method that creates
// a frame, then creates an instance of the applet and
// adds it to the frame.

public class Window2Applet extends Applet{

public void init(){
add(new Button("Hello Button"));
}

public static void main(String args[]){
// Create the frame this applet will run in
Frame myWindow = new Frame("Applet inside - Window can close ");

// Create an instance of the applet
Applet myApplet = new Window2Applet();

// Initialize and start the applet
myApplet.init();
myApplet.start();

// Add the applet to the frame
myWindow.add(myApplet);
//Handle Window close message
myWindow.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent event){
System.exit(0);
}
});

// Have to give the frame a size before it is visible
myWindow.setSize(new Dimension(300, 100));

// Make the frame appear on the screen
myWindow.show();
}
}