|
"Hello World" Applet/Application
java myAppletPackage.HelloWorldApplet2 package myAppletPackage; import java.applet.Applet; import java.awt.BorderLayout; import java.awt.Button; import java.awt.Dimension; import java.awt.Frame; import java.awt.Toolkit; // //This shoe horns a Java applet into an application. //The resulting application does not immediately take //advantage of what being an application can offer, such as: // less security restrictions // create blank Images of arbitrary size // public class HelloWorldApplet2 extends Applet { public HelloWorldApplet2() { //applet constructor } public void init() { //applet init code setSize(200, 400); //give applet some initial size setLayout(new BorderLayout()); add(new Button("Hello World!"), "Center"); //something interesting in applet } public void start() { //applet startup code } public static void main(String[] args) { Applet applet = new HelloWorldApplet2(); //create applet Frame application = new Frame("My Application Name"); //create application frame application.add(applet); //shoehorn applet into frame applet.init(); //initialize applet applet.start(); //start the applet Toolkit tk = Toolkit.getDefaultToolkit(); //grab toolkit to calculate screen size Dimension theScreen = tk.getScreenSize(); //center the application according to its size Dimension appletDimension = applet.getSize(); application.setBounds((theScreen.width - appletDimension.width) / 2, (theScreen.height - appletDimension.height) / 2, appletDimension.width, appletDimension.height); application.setVisible(true); //show the application (default = hidden) } } |