Hello World Program Using SWING

hello friends Today I am discussing about Hello world program using swing
let look below is the program codes
import javax.swing.jFrame;
import javax.swing.JLabel;

public final class hello World extends JFrame
{
           private Hello World()
           {
           super("Hello, World");
           setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
           
           getContentPanel().add(new JLabel("Hello, World"));
           pack();
           }
         public static void main(String[] args)
         {
         new HelloWorld().setVisible(true);
         }
}

now let understand this program
Firstly the constructor of super class is called to set the window's title as "Hello, World!".
Swing has its own dispatch thread, which is distinct from the main JVM thread.
         This means that even if the thread exited main method the swing GUI thread world still be running waiting to reponds to user input etc and if the user closes the window then the program would continew to run .Previously we typically attached a window's listener to our main Jframe and on windowsClosing() event notification callback we would explicitly call System.exit(stat) or some other nastiness.This is the new way of defining default close behavior for a JFrame .The following will merely dipose the JFrame but it will not stop the application

            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
the following stops the application on window close(it is commented out.)
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Jframe which is the window component of swing, has an intesering feature it has a layered content architecture which allows for  placement of a component of the JFrame in a designated layer .The layers can be conceptually viewed as a seried of transparencies placed one above another in a overhead projector Swing.



pack( ) tells the receiving container (here this Jframe) for it to optimally pack its components according to its layout


For further knowledge Use can see CLICK HERE

enjoy...keep visiting

Hello World Program Using SWING Hello World Program Using SWING Reviewed by Unknown on 03:03 Rating: 5

No comments:

Follow and +1 Us

Powered by Blogger.