Friday, March 20, 2020

Creation of GUI for client

A graphical user interface starts with a top-level container which provides a home for the other components of the interface, and dictates the overall feel of the application. We have used the JFrame class, which is used to create a simple top-level window for a Java application.

To use the Jframe you need to follow following steps:

1. Import the Graphical Components

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;

Java comes with a set of code libraries designed to help programmers quickly create applications. They provide access to classes that perform specific functions, to save you the bother of having to write them yourself. The two import statements above let the compiler know that the application needs access to some of the pre-built functionality contained within the "AWT" and "Swing" code libraries.
AWT stands for “Abstract Window Toolkit.” It contains classes that programmers can use to make graphical components such as buttons, labels and frames. Swing is built on top of AWT, and provides an additional set of more sophisticated graphical interface components. With just two lines of code, we gain access to these graphical components, and can use them in our Java application.


2. Create the Application Class
Enter the class definition that will contain our Java application code. 
public class InterfaceClass extends JFrame implements ActionListener{.............}
All the rest of the code goes between the two curly brackets. The InterfaceClass class is like the covers of a book, it shows the compiler where to look for the main application code.

3. Create the Function that Makes the JFrame
Group all the Java code that deals with creating the window into one function.
Enter the LoginPage() function definition:
private void LoginPage()() {..........}
All the code to create the window for Login page goes between the function’s curly brackets. Anytime the  LoginPage() function is called, the Java application will create and display a window using this code.
Now, let's look at creating the window using a JFrame object. Type in the following code, remembering to place it between the curly brackets of the LoginPage() function:
Jframe fm = new JFrame();
What this line does is create a new instance of a JFrame object called "fm". You can think of "fm" as the window for our Java application.
The JFrame class will do most of the work of creating the window for us. It can be used to set  its attributes, such as its general appearance, its size, what it contains, and more.

4. Add a JLabel to the JFrame
Since an empty window has little use, let's now put a graphical component inside it. Add the following lines of code to the  LoginPage() function to create a new JLabel object
                 JLabel luser = new JLabel("User Name : ");
luser.setBounds(130,60,150,30);

A JLabel is a graphical component that can contain an image or text. It is filled with the text “User Name :” and its size has been set by setBounds.
Now that we have created the JLabel, add it to the JFrame:
fm.add(luser);
this will add our JLabel to th JFrame.
In the next blog we have shared the entire java code of database for LAN based examination system.

Thankyou!!

15 comments: