Different Ways to Create a JFrame
Before building bigger GUIs, you should understand a few common patterns for creating the main window (JFrame). All three methods below are valid; we will primarily follow Method 2 in this documentation.
Method 1: All in main method
In the first style, everything is written inside the main method of a single class. This is very common in introductory examples and for quick testing.
import javax.swing.*;
public class FrameMethod1 {
public static void main(String[] args) {
JFrame frame = new JFrame("Method 1: All in main");
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}- Everything related to the GUI is inside the
mainmethod. - Very simple to read and write for small programs.
- Becomes messy when the GUI grows (many components and event handlers inside one method).
Method 2: Separate GUI class with constructor
In the second style, we create a separate class that extends JFrame. The constructor of that class sets up the window. The main method just creates an object.
import javax.swing.*;
class MyFrame2 extends JFrame {
MyFrame2() {
setTitle("Method 2: Constructor in separate class");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
public class FrameMethod2 {
public static void main(String[] args) {
new MyFrame2(); // call constructor
}
}- GUI setup is grouped inside a dedicated class, which keeps responsibilities clear.
- Better structure for larger applications: you can put fields, helper methods, and event handlers inside the GUI class.
- This is the main pattern we will follow in the rest of the GUI documentation.
Method 3: Using a method to create the GUI
The third style extracts JFrame creation into a separate method (often called createAndShowGUI). The main method simply calls it.
import javax.swing.*;
public class FrameMethod3 {
// Method to create JFrame
public static void createAndShowGUI() {
JFrame frame = new JFrame("Method 3: Method for GUI");
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
createAndShowGUI(); // call method
}
}- Separates GUI creation into a method so that
mainstays clean and easy to read. - Useful if you need to call the GUI setup method from different contexts or for multiple windows.
- Also a common style in official Swing examples (often combined with the Event Dispatch Thread).
Comparison and Which Method We Use
All three methods create exactly the same window on the screen. The difference is in how we organize code. For exam answers and clean projects, it is important to show a good structure.
Method 1
- All code in
main. - Fast to write.
- Hard to maintain for big GUIs.
Method 2
- Separate GUI class.
- Good OOP style.
- Easy to extend and reuse.
Method 3
- Dedicated GUI method.
- Very clean main method.
- Flexible for advanced patterns.
In this documentation, we will mostly use Method 2 (separate GUI class with constructor) when building examples, because it matches object-oriented design and keeps programs organized.