How To Make A GUI(Graphical User Interface) In Java

first java application

heyy guys !! i am back with java .. , so today i am gonna teach you how to make a your very first java GUI program after that i will write more tutorial on adding controls to the interface and giving click events .. , first of all open your favorite text editor and after that these are the codes i have written so go ahead and put them in



import javax.swing.*;

/** 
 * Author : Mohamed Shimran
 * Blog : http://www.ultimateprogrammingtutorials.blogspot.com
 */

public class GUI1 extends JFrame
{
public GUI1()
{
setSize(500,400);
setDefaultCloseOperation(3);
setVisible(true);
setTitle("My first GUI application in java");
}

public static void main(String args[])
{
new GUI1();
}
}

ok now as you can see import javax.swing.*; is a namespace like we use in vb.net as imports system.io and in c# we use using System.IO; the namespace i have used here is really needed to create a GUI program .
then we have public class GUI1 extends JFrame normally when we create CLI programs in java we dont use that extends JFrame that JFrame is the GUI in java after that we have to set the properties of our GUI program as you can see i have written setSize(500,400); that is the size of our form after that you can see setDefaultCloseOperation(3); that is actually useful when you click on close button in the program that piece of code helps you to close ot permenantly if you don't have that code in your program when you click close the program will run in the processes , now setVisible(true); you can understand what that means if you don't want to show the form use false ,then we have title("....") you can add your title there for the program .. as then we have our normal line now don't forget that you have to use the same name in three places as you can see i have my class name GUI1 and i have it in three places all together . compile and run and have fun .

comment below if you have any problems in this , share with your friends

Post a Comment

Note: Only a member of this blog may post a comment.