Jframe Java
Certainly! A JFrame in Java is a top-level window that serves as the main container for building graphical user interface (GUI) applications using the Swing library. Here’s a basic example of how to create a JFrame in Java:
import javax.swing.*;
public class MyFrame {
public static void main(String[] args) {
// Create a JFrame instance
JFrame frame = new JFrame("My First JFrame");
// Set the size of the JFrame (width, height)
frame.setSize(400, 300);
// Set the default close operation to exit the application when the frame is closed
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a JLabel to add some text to the frame
JLabel label = new JLabel("Hello, JFrame!");
// Add the label to the content pane of the frame
frame.getContentPane().add(label);
// Make the JFrame visible
frame.setVisible(true);
}
}
In this example:
We import the
javax.swing.JFrameclass and other necessary Swing components.We create a class called
MyFrame.In the
mainmethod, we create an instance ofJFramenamedframe.We set the size of the
JFrameusing thesetSizemethod, specifying the width and height in pixels.We set the default close operation using
setDefaultCloseOperationtoJFrame.EXIT_ON_CLOSE, which means the application will exit when the frame is closed.We create a
JLabelwith the text “Hello, JFrame!”.We add the label to the content pane of the frame using
getContentPane().add(label).Finally, we make the
JFramevisible withsetVisible(true).
Demo Day 1 Video:
Conclusion:
Unogeeks is the No.1 Training Institute for Java Training. Anyone Disagree? Please drop in a comment
You can check out our other latest blogs on Java Training here – Java Blogs
You can check out our Best in Class Java Training details here – Java Training
Follow & Connect with us:
———————————-
For Training inquiries:
Call/Whatsapp: +91 73960 33555
Mail us at: info@unogeeks.com
Our Website ➜ https://unogeeks.com
Follow us:
Instagram: https://www.instagram.com/unogeeks
Facebook: https://www.facebook.com/UnogeeksSoftwareTrainingInstitute
Twitter: https://twitter.com/unogeeks