Ask Questions?

View Latest Questions


 
 

First Window
Posted on: July 22, 2006 at 12:00 AM
This is about the simplest GUI (Graphical User Interface) window that you can make. The window is very small, and will appear in the top left corner of the screen.

Java Notes

Example - First Window

This is about the simplest GUI (Graphical User Interface) window that you can make. The window is very small, and will appear in the top left corner of the screen, so you may not see it at first.
  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
//--- basic_windows/FirstWindow.java - Creates a very small window!
// This is just about the smallest GUI program you can write.
// It does nothing, but you can see that the close box works
// and the window can be resized.
// Fred Swartz 2004-10-26

import javax.swing.*;

////////////////////////////////////////////////////// class FirstWindow
class FirstWindow {
    // ===================================================== method main
    public static void main(String[] args) {
        JFrame window = new JFrame("My First Window");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setVisible(true);
    }
}

Notes referencing line numbers

  1. Comments: Every source file must start with comments - for human readers, not the compiler. Include the name of the file, a short description, your name, and the date. More extensive descriptions are needed for larger programs.
  2. Imports - The import statement specifies library classes that are needed. This statement imports all classes from the javax.swing package, altho only the JFrame class is used.
  3. Class definition - The name of the source file must be identical to the class name including case, plus a .java extension.
  4. Main - The header for the main program must look like this.
  5. JFrame is the Java class for