Swing Applets and Stand-Alone Applications

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.

How this is done depends on the type of program:

Applets

An applet is displayed in a browser, which uses your HTML file to meet most of the requirements.

  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 does this automatically.

Stand-Alone Applications

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 JFrame subclass. The size is set with the setSize() method or the pack() method. The setSize() method has two int parameters, specifying the width and height in pixels. The pack() method has no parameters. The position is set with the setPosition() method. The setPosition() method has two int parameters specifying 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 JFrame subclass. The window is made visible with a setVisible(true) message.

In addition, it is desirable to have the application respond to a user click on the close button. This can be accomplished by sending the following message to the frame.

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      

Combined Applets and 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.

A template shows the organization of the main() method code.

Template for Combining Applets and Stand-Alone Applications

The following template shows the code that is needed to combine applets and stand-alone applications. It should be added to the applet class. Just substitute the data that you want for the blue 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());

      // Make the application shut down when the user clicks
      // on the close button.
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      // 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.setVisible(true);

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

    }