Graphical user interphase in java
Definition
A Graphical User Interface (GUI) is a visual way of interacting with a computer program. It
allows users to communicate with software through graphical components such as
windows, buttons, icons, text fields, and menus. Instead of typing text commands, users
can click, drag, or select items on the screen.
Formal Definition
A Graphical User Interface (GUI) is a visual interface that enables the user to interact with
a program through graphical elements such as buttons, windows, icons, and menus. In
Java, GUI applications are created using frameworks like AWT, Swing, and JavaFX, which
provide pre-built classes and components for building interactive and user-friendly
applications.
Purpose of GUI
The main purpose of a GUI is to make computer interaction easier and more intuitive. It
hides complex commands behind simple visual elements, making applications accessible
even to non-technical users.
Working Principle
GUI systems follow an event-driven programming model. This means the program waits
for events (like button clicks, mouse movement, or key presses) and responds
accordingly.
Components of a GUI
A GUI is made up of multiple graphical components, also called widgets or controls.
Each serves a specific purpose.
Component Description Example
Frame Main window that holds other components Application window
Janel Sub-container inside a frame Section for grouping elements
JButton Button used to trigger an action “OK”, “Submit”, “Login”
Label Displays text or images “Enter your name:”
Eastfield Single-line text input Name or Email field
Texture multi-line text input Feedback box
Checkbook Used for Yes/No or True/False options “Remember Me”
CompuBox Drop-down list of items Select Country
Radiomutation Grouped selection options Gender selection
Structure of a GUI Program
A Java GUI program has three main parts: 1. Container – The window that holds
components (like Frame) 2. Components – The visible items (buttons, labels, etc.) 3.
Event Handling – The mechanism that reacts to user actions.
Advantages of GUI
1. User-Friendly – Easy for beginners. 2. Interactive – Immediate response to actions. 3.
Error Reduction – Fewer input errors. 4. Attractive Design – Better visual layout. 5.
Multi-Device Support – Works across platforms.
Disadvantages of GUI
1. Higher Memory Usage. 2. Slower Speed. 3. Complex Design. 4. Difficult Debugging. 5.
Limited Control for low-level tasks.
Window in Graphical User Interface (GUI)
In Java, a window is created using the Frame class from the Frame package.
A Frame acts as the main container that holds all GUI components like buttons, labels, and
text fields.
Every GUI application must have at least one main window.
Steps to Create a Window
Import the Swing package
Create a Frame object
Set the window size
Define the close operation
Make the window visible
(Optional) Center or style the window
Complete Example
Output
A window titled “My First GUI Window” will appear.
You can move, resize, minimize, or close it like any normal window.
Frame
A Frame is a top-level window provided by the Swing library in Java, which acts as the
main container for other GUI components.
It represents the main window of a desktop application.
All interactive graphical components such as buttons, labels, panels, and text fields are
placed inside a Frame.
Purpose of Frame
A Frame:
Creates the main window of your application.
Provides methods to control window size, title, closing behavior, and visibility.
Acts as a container to hold other GUI elements.
Supports event handling, so the program can respond to user actions (clicks, typing,
etc.).
Can be customized using layout managers, background colors, icons, and more.
Commonly Used Methods in j frame
set Title () – sets the title of the window.
set Size () – sets the size of the window.
set Visible () – shows or hides the window.
set Resizable () – allows or prevents resizing.
Resizable (() – defines what happens when the user closes the window.
(() () – sets the position of the window (usually to center).
add () – adds components to the frame.
Two Ways to Create a Window in Java (Using Frame)
By Creating an Object of Frame Class
You can declare and create an object of the Frame class directly.
After creating the object, you can use different Frame methods (like set Size (), set
Title (), set Visible (), etc.) to design and control the window.
Example idea:
Create a Frame object → Set window title and size → Display the window.
This is the simpler method, mainly used for small programs or quick GUIs.
🔹 2. By Extending the Frame Class (Using Inheritance)
You can also create your own class that extends (inherits from) the Frame class.
This method is object-oriented and allows better structure for larger GUI
programs.
When a class extends Frame, it inherits all the features (methods and variables) of
Frame.
You can also add your own new features or functions inside this new class.
This approach uses inheritance, meaning the new class is built on top of an
existing class (Frame).
Example idea:
A new class like Rectangle Program can extend Frame.
It can use Frame features (like creating a window) and add extra code (like
calculating area and perimeter).
This method is more powerful and flexible, suitable for full applications.
Inheritance and Creating a Window (Using Frame)
public class Rectangle Program extends Frame
public Rectangle Program () //constructor
{//Necessary code
public static void main (String [] rags)
{//Code for the method main
}
1. The keyword extends is used in Java to show inheritance.
2. When a class extends another class, it becomes a subclass and inherits all public
methods and properties of the superclass.
3. In this example, Rectangle Program is a subclass of Frame.
4. Because of inheritance, Rectangle Program can use all methods of Frame directly.
5. The class contains:
o A constructor Rectangle Program () – used to create and set up the window.
o A main method main () – where the program starts execution.
6. Important Frame methods that can be used inside the constructor:
o set Title ("Area and Perimeter of a Rectangle"); → sets the title of the window.
o set Size (width, height); → defines the size of the window.
o set Visible(true); → displays the window on the screen.
o (EXIT_ON_CLOSE); → closes the program when the window is closed.
Label | Java Swing
o Label is a class of java Swing. Label is used to display a short string or an image
icon. Label can display text, image or both. Label is only a display of text or image
and it cannot get focus. Label is inactive to input events such a mouse focus or
keyboard focus. By default, labels are vertically centered but the user can change
the alignment of label.
o Purpose
o To show text messages, titles, or instructions to users in a GUI.
o Commonly used beside text fields, buttons, or images.
o
Constructor of the class are:
o Label (): creates a blank label with no text or image in it.
o Label (String s): creates a new label with the string specified.
o Label (Icon I): creates a new label with an image on it.
o Label (String s, Icon I, int align): creates a new label with a string, an image and a
specified horizontal alignment
o
Commonly used methods of the class are:
o get Icon (): returns the image that the label displays
o set Icon (Icon I): sets the icon that the label will display to image I
o get Text (): returns the text that the label will display c
o set Text (String s): sets the text that the label will display to string s
o
JButton in Java
🔹 Definition
JButton is a GUI component in Java used to create clickable buttons.
It is part of the Swing library ([Link] package).
When the user clicks a button, it can perform an action, such as displaying a message,
opening a new window, or performing calculations. JButton is used for user
interaction.
points
Belongs to [Link] package.
Needs ActionListener for click handling.
Supports text, images, icons, and tooltips.
Commonly used in forms, menus, and dialogs.
🔹 Purpose
To let the user interact with a GUI by clicking.
Used for event handling, e.g., “Submit”, “OK”, “Exit”, etc.
Constructors of JButton
Constructor Description
JButton() Creates an empty button.
JButton(String text) Creates a button with the given text.
JButton(Icon image) Creates a button with an image.
JButton(String text, Icon image) Creates a button with both text and image.
🔹 Common Methods
Method Description
setText(String text) Changes the button text.
getText() Returns the button text.
setEnabled(boolean b) Enables or disables the button.
setIcon(Icon i) Sets an image icon on the button.
setToolTipText(String t)
Displays a message when the mouse hovers over the
button.
Method Description
Adds an event listener to detect button clicks.
addActionListener(ActionListener
l)
Event Handling in JButton
To make a button perform an action when clicked, we use an ActionListener.
The ActionListener interface has one method:
example
public void actionPerformed(ActionEvent e)
When the button is clicked, this method is automatically called.