Home Java Java-tips GUI Tutorial Example - Tiny Window

Ask Questions?

View Latest Questions


 
 

Example - Tiny Window
Posted on: July 26, 2006 at 12:00 AM
To display the Tiny Window

Java Notes

Example - Tiny Window

The example discussed here is used to create tiny window in swing.

The TinyWindow.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.

 

You can download the code given here and try on your machine.

 

We have also given the screen shot of the example program.

 

Here is the screen shot and description of the code.

 

A very small window The image on the left is the window in its "natural" size. There's nothing in the window so it shrinks down to almost nothing. Because the default position is in the upper left corner of the screen, it may be hard to even see that it's running.

The basic window (JFrame) has controls that work, with the exception of the close box which you must indicate should stop the program as in the program below. You can iconify it, and resize it. The image on the right is what you get if you drag the lower-right corner to enlarge the window. The content pane is just a plain background with nothing on it.

A very small window
  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
//--- intro-windows/TinyWindow.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-28

import javax.swing.*;                                           //Note 1

////////////////////////////////////////////////////// class TinyWindow
class TinyWindow {
    // ===================================================== method main
    public static void main(String[] args) {
        JFrame window;                                          //Note 2
        window = new JFrame();                                  //Note 3
        window.setTitle("Tiny Window");                         //Note 4
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  //Note 5
        window.setVisible(true);                                //Note 6
    }
}

Notes



Note 1: This import statement gets all of the Swing classes, altho we are only using JFrame here. Normally there will be a couple of other imports to get the rest of the common GUI classes.

Note 2: Our common idea of "window" is implemented by the JFrame class.

Note 3: Create a JFrame object..

Note 4: Set the text on the title bar.

Note 5: Makes the application quit when the close box is clicked.

Note 6: After everything is ready, display the window on the screen. This also starts a separate thread to monitor the user interface. Note that System.exit(0) is not called; we don't want to terminate the program. The typical GUI program builds the user interface, then just waits until the user does something.

Copyleft 2004 Fred Swartz MIT License

Related Tags for Example - Tiny Window:
cguiexceptionswingcomimageideclassrestormcontrolsuiimportcontrolioclassesresizesizebackgroundgetwindowscreenicondefaultportcontentstateusingthisidaipositionjframebasiccloselargeframeifboxwithworkdragprogramstatementnattostopramroundposrunsseldportsshtopeilitbacknotlscanuseulpeimcefaultleftinnormasstamnttrthinososicaosiosjnothingcornerclesrunningshrinkemcommonallalmaltageicomehrpropppanackcatdodownrightwhichwerssoeeatpanekideaimportsisinkhaimplementedllnotemplgetseaexceptandarstatclosrtrtswingzscrssrirdthshoswbelostargehatefaultesiframesimageindichardicaicapleplplaiprmostndonlyonomogrolonormnormanlnlanor