How to develop simple GUI(Graphical user interface) application in java - The Idle Coder

How to develop simple GUI(Graphical user interface) application in java

Hello there...In this tutorial you will get the basic concept of GUI in Java and you will be able to make a simple GUI application in Java. Let's start with the first question!
What is GUI(Graphical unit interface)?
In simple words,we can say GUI can be defined as the type of interface that allows users to interact with electronic devices through graphical icons and visual indicators..
Here we will discuss the syntax about the components that we will use to develop simple GUI application. You may have confusion, but you have to remember the syntax form and other concepts. Mainly all the components use constructors.

Also read:- Interface in java the complete guide

List of components

1. Frame:

The Frame is the basic component that is used to define frame object through which we can perform operations on other components. The syntax form is

Frame(String title);

2. TextField:

TextField is one type of box in which we can add some value through keyboard. We can define its length by giving integer value inside TextField

TextField(int num);
String getText();
void setText(String s);

3. TextArea

TextArea is used generally to write the address of the long form of text inside box. We can easily define its height and width by giving integer value
TextArea(int height,int width);

4. checkBoxGroup:

It is grouping of checkboxs. When there are more than one checkBox, it is necessary to use this component.

5. checkBox:

 It is used for radio button. It has uses boolean data type of selection process.It is allocated to particular checkBoxGroup

6 Label:

The Label is one type of static property that will not be changed during program.For example Student-name is a label

Label(String s);

7. Button:

It is used for making simple Button inside the application.
Button(String Label);

Before you proceed further towards execution, consider bellow points

  • Please make sure you have included java.awt file
  • Though Java is case sensitive you have to take care of the syntax
  • Here we are going to make a simple form in Java using GUI concepts


import java.awt.*;
class demo                                   
{                        //objects of the components are declared
Frame f;
TextField tn;
TextArea ta;
CheckboxGroup g;                          
Checkbox m;
Checkbox ff;
Button yes;
Button no;
demo()          //Default constructor
              //Objects are defined using components
f=new Frame("Demo window");

Label ln=new Label("Name: ");
tn=new TextField(5);

Label la=new Label("Address: ");
ta=new TextArea(5,10);
Label gn=new Label("Gender");                    
g=new CheckboxGroup();
m=new Checkbox("male",true,g);
ff=new Checkbox("female",false,g);
Label tex=new Label("Are you Sure?");
yes=new Button("Yes");
no=new Button("No");

f.setLayout(new FlowLayout());
class demowindow      //main class
public static void main(String args[])
demo obj=new demo();     //calls constructor


<img src="image example link" alt="How to develop simple GUI(Graphical user interface) application in java" />
Output Window


 Here our aim is to get simple form(See output image) in java using GUI concepts. First, we have imported java.awt.* file to use GUI. Inside demo class we have created the objects of components that we need. Now there is big constructor ahead! Inside constructor we have set the frame name that will be on top of the output window. We have defined the window size and set the visible property to true. So it can be visible in the output.

Also read:-Guide about methods in java

 Then we have used labels and for every label there is one component. f.setLayout(new FlowLayout()) is necessary to show the required components in the application. If you remove this and then run it you will get the last button only. So it is required to use setLayout(new FlowLayout()) using frame object. At last, in constructor all the value which is defined are added with help of f.add() method. In the main class the object is created with the help of constructor so it will call the default constructor directly .You will not get output in proper layout. That will be covered in the next tutorial. Must note that to re-execute the code you must have to close output window.

These components are basic components of the GUI in Java.We will see other components in the next tutorial. Share this tutorial if you like. Thank you!!

Next Post »