Programs to implement JFrame
1. Java Program to demonstrate a simple JFrame
Below is the demonstration of the simple JFrame:
Java
// Java Swing Program to demonstrate // a simple JFrame import javax.swing.JFrame; import javax.swing.JLabel; // Driver Class public class MyJFrame { // main function public static void main(String[] args) { // Create a new JFrame JFrame frame = new JFrame( "My First JFrame" ); // Create a label JLabel label = new JLabel( "Geeks Premier League 2023" ); // Add the label to the frame frame.add(label); // Set frame properties frame.setSize( 300 , 200 ); // Set the size of the frame // Close operation frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); // Make the frame visible frame.setVisible( true ); } } |
Output:
2. Java Program to Add a JMenuBar and JButton inside the JFrame
Below is the Implementation of the Add a JMenuBar and JButton inside the JFrame example:
Java
// Java Swing Program to Add // JMenuBar and JButton inside the JFrame import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; // Driver Class public class JFrameExample { // Main function public static void main(String[] args) { // Create the main frame JFrame frame = new JFrame( "JFrame Example" ); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize( 800 , 600 ); // Create a menu bar JMenuBar menuBar = new JMenuBar(); JMenu fileMenu = new JMenu( "File" ); JMenuItem openItem = new JMenuItem( "Open" ); JMenuItem exitItem = new JMenuItem( "Exit" ); fileMenu.add(openItem); fileMenu.addSeparator(); fileMenu.add(exitItem); menuBar.add(fileMenu); // Create a panel with a button JPanel panel = new JPanel(); JButton button = new JButton( "Click Me" ); panel.add(button); // Add action to the button button.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(frame, "Button Clicked!" ); } }); // Create another panel with text JPanel textPanel = new JPanel(); JLabel label = new JLabel( "Geeks Premier League 2023" ); textPanel.add(label); // Set layout for the main frame frame.setLayout( new BorderLayout()); frame.setJMenuBar(menuBar); frame.add(panel, BorderLayout.CENTER); frame.add(textPanel, BorderLayout.SOUTH); frame.setVisible( true ); } } |
Output:
manage
Java JFrame
Thе Java JFramе is an еssеntial componеnt of Java Swing, which is a part of thе Java SWT(Standard Widgеt Toolkit). JFrame in Java is a class that allows you to crеatе and manage a top-lеvеl window in a Java application. It sеrvеs as thе main window for GUI-basеd Java applications and providеs a platform-indеpеndеnt way to crеatе graphical usеr intеrfacеs. In Java JFrame is a part of javax.swing package. In this article, we will learn about Java JFrame.
Constructor of Java JFrame
Constructor |
Description |
---|---|
JFrame() |
This is the default constructor for JFrame. It creates a new frame with no title |
JFrame(String title) |
This constructor creates a new frame with the specified title. |
JFrame(GraphicsConfiguration gc) |
This constructor creates a JFrame that uses the specified graphics configuration. |
JFrame(String title, GraphicsConfiguration gc) |
This constructor creates a JFrame with the specified title and using the specified graphics configuration. |
JFrame(Rectangle bounds) |
This constructor creates a JFrame with the specified bounds. |
JFrame(String title, Rectangle bounds) |
This constructor creates a JFrame with the specified title and bounds. |
Methods of Java JFrame
Methods |
Description |
---|---|
setTitle(String title) |
Sets the title of the JFrame. |
setSize(int width, int height) |
Sets the size of the JFrame. |
setDefaultCloseOperation(int operation) |
Sets the default close operation for the JFrame. Common options include JFrame.EXIT_ON_CLOSE, JFrame.HIDE_ON_CLOSE, and JFrame.DO_NOTHING_ON_CLOSE. |
setVisible(boolean b) |
Sets the visibility of the JFrame. Pass true to make it visible and false to hide it. |
setLayout(LayoutManager manager) |
Sets the layout manager for the JFrame, which controls how components are arranged within the frame. |
add(Component comp) |
Adds a Swing component to the JFrame. |
remove(Component comp) |
Removes a component from the JFrame. |
validate() |
Forces the layout manager to recalculate the layout of components within the JFrame. |
setResizable(boolean resizable) |
Controls whether the user can resize the JFrame. |
setIconImage(Image image) |
Sets the icon (image) for the JFrame window. |
Parent Classes of JFrame Methods
Java JFrame is the part of Java Swing and the classes from where all the methods associated with JFrame are inherited are mentioned below:
- java.awt.Frame
- java.awt.Container
- java.awt.Window
- javax.swing.JFrame
Some Fields for Java JFrame
Fields |
Description |
---|---|
EXIT_ON_CLOSE (int) |
A field that specifies the default operation to perform when the user closes the JFrame. It’s typically used with the setDefaultCloseOperation() method. |
DISPOSE_ON_CLOSE (int) |
It is constant that specifies an operation to perform when the user closes the JFrame.It disposes of the JFrame but doesn’t exit the application. |
HIDE_ON_CLOSE (int) |
Specifies that the JFrame should be hidden but not disposed of when the user closes it. |
DO_NOTHING_ON_CLOSE (int) |
Indicates that no action should be taken when the user closes the JFrame. |