Curriculum
Course: Learn Java Programming
Login

Curriculum

Learn Java Programming

Video lesson

Creating a JFrame Using Swing in Java

 

In this lesson, you will learn.

  • Working with JFrame
  • Constructors and Methods of JFrame
  • Examples

 

What is a JFrame

  1. JFrame is a class in Java’s Swing library, which is used to create a window where graphical user interface (GUI) components like buttons, labels, and text fields can be added.
  2. It is a fundamental part of creating desktop applications in Java.

 

Constructors of JFrame Java

  1. JFrame(): Creates a new, initially invisible frame with no title.
  2. JFrame(String title): Creates a new, initially invisible frame with the specified title.

 

Example-1: Creating a Frame

package jframes;

import javax.swing.JFrame;

public class MyFrame extends JFrame {
    
    public MyFrame() {
        // Set the title of the JFrame
        setTitle("My First JFrame");
        /*
         * Set the default close operation so the application 
           exits when the frame is closed
         */
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        // Set the size of the JFrame
        setSize(400, 300);
        
        //Set the x,y location of a Frame
        setLocation(100, 100);
        
        // Make the JFrame visible
        setVisible(true);
    }
    
    public static void main(String[] args) {
        // Create an instance of MyFrame
        new MyFrame();
    }
}

Output

 

 

The above program can be written like the following code snippets

 

Approach-1: Adding complete code inside a main() method

package jframes;
import javax.swing.JFrame;
public class MyFrame {
	public static void main(String[] args) {
		//Create an object of JFrame
		JFrame jFrame = new JFrame();
		// Set the title of the JFrame
        jFrame.setTitle("My First JFrame");
        /*
         * Set the default close operation so the application 
           exits when the frame is closed
         */
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        // Set the size of the JFrame
        jFrame.setSize(400, 300); 
        //Set the x,y location of a Frame
        jFrame.setLocation(100, 100);
        // Make the JFrame visible
        jFrame.setVisible(true);
	}
}

 

Approach-2: Add a method inside a class and call in main() method

package jframes;

import javax.swing.JFrame;

public class MyFrame {
	public static void createJFrame() {
		//Create an object of JFrame
		JFrame jFrame = new JFrame();
		// Set the title of the JFrame
		jFrame.setTitle("My First JFrame");
		/*
		 * Set the default close operation so the application 
		           exits when the frame is closed
		 */
		jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
		// Set the size of the JFrame
		jFrame.setSize(400, 300); 
		//Set the x,y location of a Frame
		jFrame.setLocation(100, 100);
		// Make the JFrame visible
		jFrame.setVisible(true);
	}
	public static void main(String[] args) {
		createJFrame();
	}
}

 

Approach-3: Create Two Separate classes

package jframes;
import javax.swing.JFrame;
class MyFrame {
	public void createGUI() {
		//Create an object of JFrame
		JFrame jFrame = new JFrame();
		// Set the title of the JFrame
		jFrame.setTitle("My First JFrame");
		/*
		 * Set the default close operation so the application 
				           exits when the frame is closed
		 */
		jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
		// Set the size of the JFrame
		jFrame.setSize(400, 300); 
		//Set the x,y location of a Frame
		jFrame.setLocation(100, 100);
		// Make the JFrame visible
		jFrame.setVisible(true);
	}
}
public class MyFrameMain {
	public static void main(String[] args) {
		MyFrame myFrame = new MyFrame();
		myFrame.createGUI();
	}
}

 

Summary of Common Methods in JFrame

Method Name Description
void setTitle(String title) Sets the title for the frame.
String getTitle() Gets the title of the frame.
void setSize(int width, int height) Sets the size of the frame.
void setSize(Dimension d) Sets the size of the frame using a Dimension object.
void setVisible(boolean b) Makes the frame visible or invisible.
void setDefaultCloseOperation(int operation) Sets the operation that will happen by default when the user initiates a “close” on this frame.
void setLocation(int x, int y) Sets the location of the frame relative to the screen.
void setLocation(Point p) Sets the location of the frame using a Point object.
void setBounds(int x, int y, int width, int height) Sets the size and location of the frame.
void add(Component comp) Adds a component to the frame.
void remove(Component comp) Removes the specified component from the frame.
void setResizable(boolean resizable) Sets whether this frame is resizable by the user.
boolean isResizable() Returns whether this frame is resizable by the user.
void setIconImage(Image image) Sets the image to be displayed as the icon for this frame.
Image getIconImage() Returns the icon image for this frame.

 

 

 


 

End of the lesson….enjoy learning

 

 

Student Ratings and Reviews

 

 

 

There are no reviews yet. Be the first one to write one.

 

 

Submit a Review