JFrame Basics - potatoscript/JavaSwing GitHub Wiki
🖥️ JFrame Basics in Java Swing 🎨
🎯 What is JFrame?
JFrame
is the main window for Java Swing applications. It provides the basic framework for adding GUI components such as buttons, labels, text fields, and other elements.
✅ Key Features:
- 🖼️ Window Creation: Main container where GUI components are added.
- ❌ Close Operations: Handles application closing when the window is closed.
- 🎨 Customizable Layouts: Supports layout managers to organize GUI components.
- 🔥 Resizable & Draggable: Provides window resizing and dragging capabilities.
📚 Step 1: Creating a Basic JFrame
📝 Basic Syntax
import javax.swing.JFrame;
public class MyFrame {
public static void main(String[] args) {
// Create JFrame
JFrame frame = new JFrame("My First JFrame");
// Set frame size
frame.setSize(400, 300);
// Set default close operation
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Make the frame visible
frame.setVisible(true);
}
}
🎨 Explanation
✅ JFrame frame = new JFrame("My First JFrame");
- Creates a new
JFrame
with the title"My First JFrame"
.
✅ frame.setSize(400, 300);
- Sets the width to 400 pixels and height to 300 pixels.
✅ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- Closes the application when the window is closed.
- Options available:
JFrame.EXIT_ON_CLOSE
– Exit the application.JFrame.HIDE_ON_CLOSE
– Hide the window but keep the application running.JFrame.DISPOSE_ON_CLOSE
– Close the window but release only the resources of that frame.
✅ frame.setVisible(true);
- Displays the window on the screen.
👩🎨 Output:
+------------------------+
| |
| My First JFrame |
| |
+------------------------+
🎨 Step 2: JFrame with Basic Components
📝 Adding JLabel to JFrame
import javax.swing.*;
public class MyFrame {
public static void main(String[] args) {
// Create JFrame
JFrame frame = new JFrame("JFrame with Label");
// Create JLabel
JLabel label = new JLabel("🎉 Welcome to Swing!", SwingConstants.CENTER);
// Add label to frame
frame.add(label);
// Set frame size and behavior
frame.setSize(400, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
🎨 Explanation
✅ JLabel label = new JLabel("🎉 Welcome to Swing!", SwingConstants.CENTER);
- Creates a label with the text
"🎉 Welcome to Swing!"
and centers it horizontally.
✅ frame.add(label);
- Adds the label to the frame.
✅ frame.setSize(400, 200);
- Sets the window size to 400x200 pixels.
👩🎨 Output:
+--------------------------------+
| |
| 🎉 Welcome to Swing! |
| |
+--------------------------------+
🧩 Step 3: JFrame Properties and Methods
📝 Common JFrame Methods
Method | Description | Example Code |
---|---|---|
setSize(int width, int height) |
Sets the frame size in pixels. | frame.setSize(500, 300); |
setTitle(String title) |
Sets the title of the frame. | frame.setTitle("My Frame"); |
setVisible(boolean b) |
Makes the frame visible or hidden. | frame.setVisible(true); |
setDefaultCloseOperation(int operation) |
Sets what happens when the window closes. | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
setResizable(boolean b) |
Enables/disables frame resizing. | frame.setResizable(false); |
setLocation(int x, int y) |
Sets frame position on the screen. | frame.setLocation(200, 100); |
setLocationRelativeTo(null) |
Centers the frame on the screen. | frame.setLocationRelativeTo(null); |
pack() |
Automatically sizes the frame to fit components. | frame.pack(); |
dispose() |
Closes the frame and releases resources. | frame.dispose(); |
📦 Step 4: Adding More Swing Components
🔥 Example: Adding Multiple Components to JFrame
import javax.swing.*;
public class MyFrame {
public static void main(String[] args) {
// Create JFrame
JFrame frame = new JFrame("Multiple Components Example");
// Create JLabel
JLabel label = new JLabel("Enter your name:");
// Create JTextField
JTextField textField = new JTextField(20);
// Create JButton
JButton button = new JButton("Submit");
// Set layout
frame.setLayout(new java.awt.FlowLayout());
// Add components to frame
frame.add(label);
frame.add(textField);
frame.add(button);
// Set frame properties
frame.setSize(300, 150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
🎨 Explanation
✅ JLabel label = new JLabel("Enter your name:");
- Displays the text
"Enter your name:"
.
✅ JTextField textField = new JTextField(20);
- Creates a text field with a width of 20 columns.
✅ JButton button = new JButton("Submit");
- Creates a button with the text
"Submit"
.
✅ frame.setLayout(new java.awt.FlowLayout());
- Sets a simple layout that arranges components in a row.
✅ frame.add(label);
- Adds components to the frame.
👩🎨 Output:
+--------------------------------+
| Enter your name: ___________ |
| |
| [Submit] |
+--------------------------------+
🎮 Step 5: JFrame Layout Managers
🎛️ What is a Layout Manager?
A Layout Manager controls the arrangement and positioning of components within a JFrame
.
Layout Manager | Description | Usage Example |
---|---|---|
FlowLayout |
Arranges components in a row. | frame.setLayout(new FlowLayout()); |
BorderLayout |
Divides the frame into 5 regions. | frame.setLayout(new BorderLayout()); |
GridLayout |
Arranges components in a grid. | frame.setLayout(new GridLayout(2, 2)); |
BoxLayout |
Arranges components vertically/horizontally. | frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS)); |
GridBagLayout |
Advanced layout for complex GUIs. | frame.setLayout(new GridBagLayout()); |
🎨 Example: Using BorderLayout
import javax.swing.*;
import java.awt.*;
public class BorderLayoutExample {
public static void main(String[] args) {
JFrame frame = new JFrame("BorderLayout Example");
// Create buttons
JButton button1 = new JButton("NORTH");
JButton button2 = new JButton("SOUTH");
JButton button3 = new JButton("EAST");
JButton button4 = new JButton("WEST");
JButton button5 = new JButton("CENTER");
// Set BorderLayout
frame.setLayout(new BorderLayout());
// Add buttons to regions
frame.add(button1, BorderLayout.NORTH);
frame.add(button2, BorderLayout.SOUTH);
frame.add(button3, BorderLayout.EAST);
frame.add(button4, BorderLayout.WEST);
frame.add(button5, BorderLayout.CENTER);
// Set frame properties
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
🎨 Output:
+--------------------------+
| NORTH |
+------+------------+-------+
| WEST | CENTER | EAST |
+------+------------+-------+
| SOUTH |
+--------------------------+
🚀 Step 6: Handling Window Events in JFrame
📝 Handling Window Close Events
import javax.swing.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class WindowEventExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Window Event Example");
// Add Window Listener
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.out.println("Window is closing!");
System.exit(0);
}
});
frame.setSize(400, 200);
frame.setVisible(true);
}
}
👩🎨 Explanation
addWindowListener(new WindowAdapter() { ... })
– Listens for window events.windowClosing()
– Called when the window is about to close.
🎁 Bonus: Center JFrame on Screen
To center a JFrame on the screen:
frame.setLocationRelativeTo(null);
✅ Add this line before frame.setVisible(true);
.