Graphical User Interface (GUI)
Programming in Java
1. Introduction
A Graphical User Interface (GUI) allows users to interact with computer programs using
graphical elements such as windows, buttons, icons, and text fields. In Java, GUI
programming is achieved using the Swing library, which provides lightweight and platform-
independent components. Swing is part of the [Link] package and includes classes
such as JFrame, JLabel, JButton, and JTextField.
2. JFrame
JFrame is the top-level container used to create a window in Java applications. It belongs to
the [Link] package. All other GUI components such as buttons, labels, and text fields
are added inside a JFrame.
Key Features of JFrame:
JFrame is a subclass of [Link].
It provides a window with title bar, borders, and close/minimize/maximize buttons.
You can add multiple components to a JFrame.
Common methods include setSize(), setVisible(), setTitle(), and
setDefaultCloseOperation().
Example Program:
import [Link].*;
public class JFrameExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JFrame Example");
[Link](400, 300);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}
3. JLabel
JLabel is used to display text, images, or both on a window. It cannot receive user input and
is mainly used for showing information.
Common Methods:
setText(String text) – sets the text of the label.
setIcon(Icon image) – sets an image on the label.
setForeground(Color c) – changes text color.
setFont(Font f) – changes text font and size.
Example Program:
import [Link].*;
public class JLabelExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JLabel Example");
JLabel label = new JLabel("Welcome to Java GUI Programming!");
[Link](50, 50, 250, 30);
[Link](label);
[Link](400, 200);
[Link](null);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}
4. JButton
JButton is used to create clickable buttons in a GUI application. When clicked, an event
occurs, which can be handled using ActionListener.
Important Methods:
setText(String text) – sets the button text.
setEnabled(boolean b) – enables or disables the button.
addActionListener(ActionListener l) – adds an event listener to detect button clicks.
Example Program:
import [Link].*;
import [Link].*;
public class JButtonExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JButton Example");
JButton button = new JButton("Click Me!");
[Link](100, 80, 120, 40);
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
[Link](frame, "Button Clicked!");
}
});
[Link](button);
[Link](350, 250);
[Link](null);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}
5. Inheritance in GUI Programming
A class can extend the JFrame class to inherit its properties and methods. This allows the
program to define its own window setup inside a constructor. The 'extends' keyword is
used for inheritance.
Example Program using Inheritance:
import [Link].*;
public class RectangleProgram extends JFrame {
public RectangleProgram() {
setTitle("Area and Perimeter of a Rectangle");
setSize(400, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
RectangleProgram rectObject = new RectangleProgram();
}
}
6. Summary and Conclusion
Graphical User Interface (GUI) programming in Java allows developers to create interactive
desktop applications using Swing components. Classes like JFrame, JLabel, and JButton are
the foundation of Java GUI development. Through event handling and inheritance, complex
and user-friendly applications can be built efficiently.