Complete Java Swing GUI Guide (Beginner to
Intermediate)
This PDF is written for absolute beginners. Each GUI component is explained in simple words, with
example code and expected output. You can run these programs in BlueJ or NetBeans.
1. JFrame – Main Window
JFrame is the main window of any Java GUI application.
All other components are added inside a JFrame.
Example Code:
import [Link].*;
public class MyFrame {
public static void main(String[] args) {
JFrame f = new JFrame("My First Frame");
[Link](300,200);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}
Output:
A window of size 300x200 appears with title 'My First Frame'.
2. JPanel – Container & Background Colour
JPanel is used to hold components.
It is mainly used to set background colour.
Example Code:
import [Link].*;
import [Link].*;
public class PanelDemo {
public static void main(String[] args) {
JFrame f = new JFrame();
JPanel p = new JPanel();
[Link]([Link]);
[Link](p);
[Link](300,200);
[Link](true);
}
}
Output:
A window opens with CYAN background.
3. JButton – Button
JButton is used to perform an action when clicked.
Example Code:
import [Link].*;
public class ButtonDemo {
public static void main(String[] args) {
JFrame f = new JFrame();
JButton b = new JButton("Click Me");
[Link](b);
[Link](200,150);
[Link](true);
}
}
Output:
A button with text 'Click Me' appears on the window.
4. JLabel – Display Text
JLabel is used to display text or images.
Example Code:
import [Link].*;
public class LabelDemo {
public static void main(String[] args) {
JFrame f = new JFrame();
JLabel l = new JLabel("Hello World");
[Link](l);
[Link](200,150);
[Link](true);
}
}
Output:
Text 'Hello World' is displayed in the window.
5. JTextField – Single Line Input
JTextField is used to take single-line input from the user.
Example Code:
import [Link].*;
public class TextFieldDemo {
public static void main(String[] args) {
JFrame f = new JFrame();
JTextField t = new JTextField(15);
[Link](t);
[Link](300,150);
[Link](true);
}
}
Output:
A text box appears where the user can type.
6. JTextArea – Multi-line Input
JTextArea is used for multi-line text input.
Example Code:
import [Link].*;
public class TextAreaDemo {
public static void main(String[] args) {
JFrame f = new JFrame();
JTextArea ta = new JTextArea(5,20);
[Link](ta);
[Link](300,200);
[Link](true);
}
}
Output:
A large text area appears allowing multiple lines of text.
7. JComboBox – Drop Down List
JComboBox allows selection of one item from a list.
Example Code:
import [Link].*;
public class ComboBoxDemo {
public static void main(String[] args) {
JFrame f = new JFrame();
JComboBox cb = new JComboBox();
[Link]("Red");
[Link]("Blue");
[Link]("Green");
[Link](cb);
[Link](200,150);
[Link](true);
}
}
Output:
A drop-down list with Red, Blue, Green options appears.
8. Event Handling – ActionListener
ActionListener performs an action when a button is clicked.
Example Code:
import [Link].*;
import [Link].*;
public class EventDemo {
public static void main(String[] args) {
JFrame f = new JFrame();
JButton b = new JButton("Click");
[Link](e -> {
[Link](f, "Button Clicked");
});
[Link](b);
[Link](200,150);
[Link](true);
}
}
Output:
When button is clicked, a message box appears saying 'Button Clicked'.