// Swing Hello, world! applet
// Shows how to use swing components in an application
// and how to create that is both an applet and application
import java.applet.*; // for Applet
import java.awt.*; // for Graphics, BorderLayout
import java.awt.event.*; // for WindowAdapter
import javax.swing.*; // for JLabel, JFrame
public class MySwingHelloApplet // save this to "MySwingHelloApplet.java"
extends Applet // essential to create an applet
{
final static String STR = "Hello, world!";
public MySwingHelloApplet() // this is changed to a constructor
{
add(new JLabel(STR)); // Swing version of Label class
}
public void paint(Graphics g)
{
g.drawString(STR,200,100); // Write a string to the screen, on the fly.
try{
showStatus(STR);} // write a string to browser status bar
catch(Exception e){}
System.out.println(STR); // write string to "java console"
}
static Applet applet; // used by application
public static void main(String[] args) // not called from webbrowser
{
JFrame frame=new JFrame();
applet=new MySwingHelloApplet();
frame.getContentPane().add(applet,BorderLayout.CENTER);
frame.setSize(400,300);
frame.addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent evt)
{ applet.stop(); applet.destroy(); System.exit(0);
} } );
applet.init();
applet.start();
frame.setVisible(true);
}
}