This article is about Programming
How to Create a GUI with Java
By NIIT Editorial
Published on 27/07/2021
GUI, which stands for Graphical User Interface, is a user-friendly visual experience builder for Java applications. It comprises graphical units like buttons, labels, windows, etc. via which users can connect with an application. Swing and JavaFX are two commonly used applications to create GUIs in Java.
Elements of GUI:
A GUI comprises an array of user interface elements. All these elements are displayed when a user is interacting with an application and they are as follows:
1. Input commands such as buttons, check boxes, dropdown lists and text fields.
2. Informational components like banners, icons, labels or notification dialogs.
3. Navigational units like menus, sidebars and breadcrumbs.
GUI in JAVA: Swing and JavaFX
As mentioned above, to create a GUI in Java, Swing and JavaFX are the most commonly used applications. Swing was designed with a flexible architecture to make the elements customizable and easy to plug-and-play which is why it is the first choice for java developers while creating GUIs.
As far as JavaFX is concerned, it consists of a totally different set of graphic components along with new features and terminologies.
Creating a GUI
The process of creating a GUI in Swing starts with creating a class that represents the main GUI. An article of this class acts as a container which holds all the other components to be displayed.
In most of the projects, the main interface article is a frame, i.e., the JFrame class in javax.swing package. A frame is basically a window which is displayed whenever a user opens an application on his/her computer. It has a title bar and buttons such as minimize, maximize and close along with other features.
The JFrame class consists of simple constructors such as JFrame() and JFrame(String). The JFrame() leaves the frame’s title bar empty, whereas the JFrame(String) places the title bar to a specified text.
Apart from the title, the size of the frame can also be customized. It can be established by incorporating the setSize(int, int) method by inserting the width and height desired for the frame. The size of a frame is always designated in pixels.
For example, calling setSize(550,350) would create a frame that would be 550 pixels wide and 350 pixels tall.
Usually, frames are invisible at the time of their creation. However, a user can make them visible by using the frame’s setVisible(boolean) method by using the word ‘true’ as an argument.
The following are the steps to create GUI in Java
STEP 1: The following code is to be copied into an editor
STEP 2: Save and compile the code as mentioned above and then run it.
STEP 3: Adding buttons to the above frame. To create a component in Java, the user is required to create an object of that component’s class. We have already understood the container class JFrame.
One such component to implement is JButton. This class represents the clickable buttons. In any application or program, buttons trigger user actions. Literally, every action begins with a click; like to close an application, the user would click on the close button.
A swing can also be inserted, which can feature a text, a graphical icon or a combination of both. A user can use the following constructors:
· JButton(String): This button is labelled with a specified text.
· JButton(Icon): This button is labelled with a graphical icon.
· JButton(String,Icon): This button is labelled with a combination of text and icon.
The following code is to be copied into an editor:
STEP 4: The above is to be executed. A big button will appear on the screen.
STEP 5: A user can add two buttons to the frame as well. Copy the code given below into an editor.
STEP 6: Save, compile and run the above code.
STEP 7: Unpredicted output = ? It means that the buttons are getting overlapped.
STEP 8: A user can create chat frames as well. Below is an example of the same:
The following is the code for creating a chat frame:
It can be concluded that creating GUI in Java is a very easy and user-friendly process as the Java applications provide customization according to the requirements of the user. To learn more about Java, and to become an expert in Java development go to NIIT and check out the courses that give great insight on Java.
PGP in Full Stack Product Engineering
Become an Industry-Ready StackRoute Certified Full Stack Product Engineer who can take up Product Development and Digital Transformation projects. Job Assured Program* with a minimum CTC of ₹7LPA*
Job Assured Program*
Easy Financing Options