Swing Applets and Stand-Alone Applications


Requirements for a Swing Graphical User Interface Program

In order to set up and run a Swing graphical user interface program, you need to do the following.

  1. Create a top-level Swing window.
  2. Add components into the content pane of the window, lay them out, and define their responses to the user with listeners.
  3. Determine the size and position of the window.
  4. Make the window visible.

Meeting the Requirements for an Applet

An applet is displayed in a browser, which uses your HTML file to accomplish most of the steps above.

  1. Create a top-level window.
    Your browser constructs an instance of the class specified in the HTML file. It expects this class to be a subclass of the AWT Applet class or its JApplet subclass.
  2. Add components into the content pane of the window, lay them out, and define their responses to the user with listeners.
    Your browser send the applet an init() message. It assumes that you have set up the components, their layout, and their behavior in the init() method or its helper methods.
  3. Determine the size and position of the window.
    Your browser sets the size as specified in the HTML file. It sets the position using its own rules for laying out a web page.
  4. Make the window visible.
    Your browser sends the applet a show() message.

Meeting the Requirements for a Stand-Alone Application

A stand-alone application is displayed using a Java virtual machine, which is started up when you give the java command. The virtual machine then executes the main() method in the class specified in the command line. The instructions in this section assume that you are writing a program that is used only as a stand-alone application. If you want to use a program as both an applet and a stand-alone application, see the following section.

  1. Create a top-level window.
    The main() method should create a JFrame object or an object from a JFrame subclass.
  2. Add components into the content pane of the window, lay them out, and define their responses to the user with listeners.
    You normally add components, lay them out, and implement their behavior in the constructor of a JFrame subclass. The main() method is usually implemented in the same class.
  3. Determine the size and position of the window.
    This can be done either near the end of the JFrame subclass constructor or in the main method after constructing an instance of the JFrrame subclass. The size is set with the setSize() method and the position is set with the setPosition() method. Both of these methods have two int parameters. The parameters for setSize() specify the width and height in pixels. The parameters for setPosition() specify the x and y coordinates of the upper left corner of the frame in pixels.
  4. Make the window visible.
    This can be done either near the end of the JFrame subclass constructor or in the main method after constructing an instance of the JFrrame subclass. The window is made visible with the show() method. It has no parameters.

Running Applets as Stand-Alone Applications

If you want to run a program either as an applet or as a stand-alone application, you can begin by writing it just as an applet, then modifying it to run also as a stand-alone application.

The simplest way to modify an applet to run as a stand-alone application is to add a main() method to the JApplet subclass that defines the applet. The main() method should do the following.

The following template shows the code that is needed. Just substitute the data that you want for the italicized portions of the code.

    public static void main(String[] args) {

	// Create an instance of the applet class.
	JApplet applet = new AppletClass();

	// Send the applet an init() message.
	applet.init();

	// Construct a JFrame.
	final JFrame frame =
		new JFrame("FrameTitle");

	// Transfer the applet's context pane to the JFrame.
	frame.setContentPane(applet.getContentPane());

	// Transfer the applet's menu bar into the JFrame.
	// This line can be omitted if the applet
	// does not create a menu bar.
	frame.setJMenuBar(applet.getJMenuBar());

	// Add a window listener to the frame for shutting
	// down the application.
	frame.addWindowListener(new WindowAdapter() {
	    public void windowClosing(WindowEvent e) {
		frame.dispose();
		System.exit(0);
	    }
	});

	// Set the size of the frame.
	// To pack the frame as tightly as possible
	// replace the setSize() message with the following.
	// frame.pack();
	frame.setSize(FrameWidth, FrameHeight);

	// Set the location of the frame.
	frame.setLocation(FrameX, FrameY);

	// Show the frame.
	frame.show();

	// Invoke the applet's start() method.
	// This line can be omitted if the applet
	// does not define a start method.
	applet.start();

    }

After this code has been added to the class, you should recompile the class and execute it with the following command.

    java AppletClass