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();
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(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.
checkBoxGroup();
5. checkBox:
It is used for radio button. It has uses boolean data type of selection process.It is allocated to particular checkBoxGroup
checkBox(“name”,boolean,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
Code:
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”);
f.setSize(500,500);
f.setVisible(true);
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());
f.add(ln);
f.add(tn);
f.add(la);
f.add(ta);
f.add(gn);
f.add(m);
f.add(ff);
f.add(tex);
f.add(yes);
f.add(no);
}
}
class demowindow //main class
{
public static void main(String args[])
{
demo obj=new demo(); //calls constructor
}
}
Output:
Output Window |
Logic:
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!!