Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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.

 

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 a "window". This statement does several things.
    • It declares a variable to reference the window we're about to create.
    • new creates a new instance (object) of JFrame by calling its constructor.
    • The window title bar text is specified in JFrame constructor call.
    It's common to specify all of these steps together, altho you may also see these done separately. Eg,
    JFrame window;                      // Declare local variable to hold JFrame.
    window = new JFrame();              // Create new JFrame and save it.
    window.setTitle("My First Window"); // Set the title bar text.
  6. Close - JFrame automatically implements basic elements of a window: resizing, minimizing, titlebar, etc, however by default a close box click only closes the window, but doesn't stop the program! Calling setDefaultCloseOperation(...) with this parameter causes a user click on the close box to stop the program in addition to making the window disappear. There is also a way to make a click on the close box run a method (function) so that the user can be prompted to save work, etc.
  7. setVisible makes the window visible and starts a thread to monitor the GUI.
  8. Return from main - After the main program displays a window, it returns and the GUI just sits there waiting for the user to do something. This event-driven programming style is different than the traditional read-compute-write loop. A more realistic program has many controls (buttons, menus, etc) that allow the user to initiate many possible actions from the program. For example, when a button is clicked, a particular method that you specify would be called.

    Do not call System.exit(0) at the end of main. You want the GUI portion of the program to continue to run.

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.