Friday, February 15, 2013

Creating a Frame using Swing JFrame

import javax.swing.JFrame; //calling the JFrame class
import javax.swing.SwingUtilities; //calling the SwingUtilities class

public class FrameExample extends JFrame{ //inherit the JFrame class
 
 FrameExample(){ //Constructor
  initGui(); //calling the function initGui
 }
 
 public void initGui(){
  setTitle("Intro to JFrame"); //sets the title of the frame;
  setSize(500,300); //sets the size of the frame (width,height)
  setLocationRelativeTo(null); //center the frame 
  setDefaultCloseOperation(EXIT_ON_CLOSE); //program will terminate when click the cross button
 }
 
 public static void main(String args[]){
  SwingUtilities.invokeLater(new Runnable(){
   public void run(){
    FrameExample ex = new FrameExample(); //creating the object of FrameExample class
    ex.setVisible(true); //To show in the scrren
   }
  });
 }
}