Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
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:
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).
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.
Type | Method | Description |
---|---|---|
protected void | addImpl(Component comp, Object constraints, int index) | It adds the specified child component. |
protected JRootPane | createRootPane() | This is called by the constructor methods to create the default rootPane. |
protected void | frameInit() | This method is called by the constructors to init the JFrame properly. |
void | setContentPane(Containe contentPane) | Sets the contentPane property |
static void | setDefaultLookAndFeelDecorated(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. |
void | setIconImage(Image image) | This sets the image to be displayed as the icon for this window. |
void | setJMenuBar(JMenuBar menubar) | Sets the menubar for this frame. |
void | setLayeredPane(JLayeredPane layeredPane) | It sets the layeredPane property. |
JRootPane | getRootPane() | Returns the rootPane object for this frame. |
TransferHandler | getTransferHandler() | 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:
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.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 7th December,2024 7th December SAT&SUN (Weekend Batch) | View Details |
edureka.co