1. Home
  2. Computing & Technology
  3. Java

Create a Simple Window Using JFrame

By , About.com Guide

6 of 7

Code Checkpoint

All the Code for the Application

Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.

As we have been doing a lot of typing, this is a good point to make sure your code matches the example. Here is what your code should look like :

import java.awt.*;
import javax.swing.*;

// Create a simple GUI window
public class TopLevelWindow {

   private static void createWindow() {

      //Create and set up the window.
      JFrame frame = new JFrame("Simple GUI");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON _CLOSE);

      JLabel textLabel = new JLabel("I'm a label in the window",SwingConstants.CENTER);
      textLabel.setPreferredSize(new Dimension(300, 100));
      frame.getContentPane().add(textLabel, BorderLayout.CENTER);

      //Display the window.
      frame.setLocationRelativeTo(null);
      frame.pack();
      frame.setVisible(true);
   }

   public static void main(String[] args) {

      createWindow();

   }
}
Explore Java
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Java
  4. Tutorials
  5. Code Checkpoint

©2009 About.com, a part of The New York Times Company.

All rights reserved.