/* * =========================================================================== * HelloSwing.java : A simple Swing applet/application to demonstrate writing * a text string to a label. Notice that this program can be run as both an * applet and standalone Java program. * * Written By : Mark Austin February, 2001 * =========================================================================== */ import javax.swing.*; import javax.swing.event.*; import javax.swing.SwingUtilities; import java.awt.*; import java.awt.event.*; import java.util.*; // This is your Swing applet (by extending JApplet). public class HelloSwing extends JApplet { public void init() { // Here is the only program statement. // Create a string label to display the text. // The label is added to the content pane // of the applet (see Code Details). getContentPane().add(new JLabel( "My first applet -- let's swing!", JLabel.CENTER)); } // When you run this program as an application. // The main method to instantiates the application frame. public static void main( String[] args ) { // Create a Swing frame. JFrame f = new JFrame("My first applet -- let's swing!"); // Create an instance of the HelloSwing applet. HelloSwing helloSwingApplet = new HelloSwing(); // Initialize the applet instance. helloSwingApplet.init(); // Add the applet to the JFrame (to it's content pane). f.getContentPane().add( helloSwingApplet ); // Add the listener to close the frame. f.addWindowListener(new WindowEventHandler()); // Assign the frame size and display it. f.setSize(300, 200); // frame: width=300, height = 200 f.show(); // Display the frame } } // ================================================= // Class to close the frame and exit the application. // ================================================= class WindowEventHandler extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } }