Java/J2EE and SOA (346 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (17 Blogs)
  • Comprehensive Java Course (4 Blogs)
  • Java/J2EE and SOA (343 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

How to Create JFrame in Java?

Last updated on Jun 17,2021 21.3K Views

24 / 29 Blog from Java Programs

JFrame is a class of the javax.swing package that is extended by java.awt.frame. This is the top-level window, with border and a title bar. JFrame class has various methods which can be used to customize it. So, this article will help you get in-depth knowledge about JFrame in Java.

These are the topics to be covered in this article:

Let’s begin!

JFrame in Java: Introduction to Java JFrames

JFrame is a top-level container that provides a window on the screen. A frame is actually a base window on which other components rely, namely the menu bar, panels, labels, text fields, buttons, etc. Almost every other Swing application starts with the JFrame window. Unlike a frame, JFrame has the option to hide or close the window with the help of the method setDefaultCloseOperation(int).

How to create a JFrame?

JFrame class has many constructors that are used to create a new JFrame. You can create a JFrame using these methods:

JFrame(): This helps in creating a frame which is invisible.
JFrame(String Title): Helps in creating a frame with a title.
JFrame(GraphicsConfiguration gc): Creates a frame with blank title and the graphics configuration of screen.

Example:

JFrame F = new JFrame();
// Or overload the constructor and give it a title:
JFrame F1 = new JFrame("Red Alert!");

Now, after creating the JFrame, you need to set the size and the location. Let’s see how this can be done.

//add the frame
JFrame f = new JFrame("Red Alert!");
//set size: width, height (in pixels)
f.setSize(450, 475);
//set the location (x,y)
f.setLocation(120, 60);

Now let’s move ahead and understand the operations that can be performed on the JFrame.

JFrame in Java: Operations

TypeMethodDescription
protected voidaddImpl(Component comp, Object constraints, int index)It adds the specified child component.
protected JRootPanecreateRootPane()This is called by the constructor methods to create the default rootPane.
protected voidframeInit()This method is called by the constructors to init the JFrame properly.
voidsetContentPane(Containe contentPane)Sets the contentPane property
static voidsetDefaultLookAndFeelDecorated(boolean defaultLookAndFeelDecorated)Provides a hint as to whether or not newly created JFrames should have their Window decorations provided by the current look and feel.
voidsetIconImage(Image image)This sets the image to be displayed as the icon for this window.
voidsetJMenuBar(JMenuBar menubar)Sets the menubar for this frame.
voidsetLayeredPane(JLayeredPane layeredPane)It sets the layeredPane property.
JRootPanegetRootPane()Returns the rootPane object for this frame.
TransferHandlergetTransferHandler()Gets the transferHandler property.

Example:

import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Jpanel;
public class JFrame Edureka{
public static void main(String s[]) {
JFrame frame = new JFrame("JFrame Example");
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JLabel label = new JLabel("JFrame By Example");
JButton button = new JButton();
button.setText("Button");
panel.add(label);
panel.add(button);
frame.add(panel);
frame.setSize(200, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

Output:

Jframe Example - JFrame in Java - Edureka

With this, we come to the end of this article on “JFrame in Java“. I hope you guys are clear with everything that has been shared with you.

I hope the above-mentioned content proved to be helpful in enhancing your Java knowledge. Keep reading, keep exploring!

Also check out Java Online Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Upcoming Batches For Java Certification Training Course
Course NameDate
Java Certification Training Course

Class Starts on 28th January,2023

28th January

SAT&SUN (Weekend Batch)
View Details
Java Certification Training Course

Class Starts on 25th February,2023

25th February

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

How to Create JFrame in Java?

edureka.co