Java JFrame
JFrame in java : Introduction to java JFrame:-
JFrame is a Class of the javax.swing package that is extended by java.awt.frame Class. That adda support for the JFS/Swing Components .
JFrame works like the main window where components like labels, buttons, textFields are added to create a GUI.
JFrame is a Swing's top level container that render a window on screen.
How To Create a JFrame?
JFrame class has many Constructors that are used to create a new JFrame , You can Create a JFrame using Following methods.
JFrame ( ) :- This Function helps in creating a frame which is invisible
JFrame ( String Title ) :- This Function Helps in Creating a frame with Title.
JFrame ( GraphicsConfiguration gc ) :- create a Frame with black Title and The Graphics Configuration of Screen.
Class Declaration
Following is the declaration of java.awt.frame class.
public class Frame
extends Frame
implements windowsConstants, Accessible, RootPlaneContainer
Field
JTabel are the fields for java.awt.Component class −
protected AccessibleContext accessibleContext −The accessible context property.
static int EXIT_ON_CLOSE − The exit application default window close operation.
protected JRootPane rootPane − The JRootPane instance that manages the contentPane and optional menuBar for this frame, as well as the glassPane.
protected boolean rootPaneCheckingEnabled − If true then calls to add and setLayout will be forwarded to the contentPane.
Class Constructors
Following are the same Constructor & it's Description;
JFrame():-
Constructs a new frame that is initially invisible.
JFrame(GraphicsConfiguration gc):-
Creates a Frame in the specified GraphicsConfiguration of a screen device and a blank title.
JFrame(String title, GraphicsConfiguration gc) :-
Creates a JFrame with the specified title and the specified GraphicsConfiguration of a screen device.
JFrame(String title) :-
Creates a new, initially invisible Frame with the specified title
Methods Inherited
This class inherits methods from the following classes −
java.awt.Frame
java.awt.Window
java.awt.Container
java.awt.Component
java.lang.Object
JFrame Example:
Following is Example of JFrame ;
import javax.swing.*;
import java.awt.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Test");
frame.setSize(new Dimension(800, 600));
frame.setVisible(true);
}
}
Useful Methods
Method |
Description |
Modifier and Type |
addImpl(Component comp, Object constraints, int index) |
Adds the specified child Component. |
protected void
|
frameInit() |
Called by the constructors to init the JFrame properly. |
protected void
|
setDefaultLookAndFeelDecorated(boolean defaultLookAndFeelDecorated) |
Provides a hint as to whether or not newly created JFrames should have their Window decorations (such as borders, widgets to close the window, title...) provided by the current look and feel. |
static void
|
createRootPane() |
Called by the constructor methods to create the default rootPane. |
protected JRootPane
|
setContentPane(Containe contentPane) |
It sets the contentPane property |
void |
setIconImage(Image image) |
It sets the image to be displayed as the icon for this window. |
void |
setJMenuBar(JMenuBar menubar) |
It sets the menubar for this frame. |
void |
setLayeredPane(JLayeredPane layeredPane) |
It sets the layeredPane property. |
void |
getRootPane() |
It returns the rootPane object for this frame. |
JRootPane |
getTransferHandler() |
It gets the transferHandler property. |
TransferHandler |
Class Methods
void update(Graphics g):- Just calls paint(g).
void setTransferHandler(TransferHandler newHandler) :-
Sets the transferHandler property, which is a mechanism to support the transfer of data into this component.
protected void setRootPane(JRootPane root) :-
Sets the rootPane property.
protected void setRootPaneCheckingEnabled(boolean enabled) :-
Sets whether calls to add and setLayout are forwarded to the contentPane.
protected void addImpl(Component comp, Object constraints, int index) :-
Adds the specified child Component.
protected JRootPane createRootPane() :-
Called by the constructor methods to create the default rootPane.
protected void frameInit() :-
Called by the constructors to init the JFrame properly.
AccessibleContext getAccessibleContext():-
Gets the AccessibleContext associated with this JFrame.
Container getContentPane() :-
Returns the contentPane object for this frame.
int getDefaultCloseOperation() :-
Returns the operation that occurs when the user initiates a "close" on this frame.
JMenuBar getJMenuBar() :-
Returns the menubar set on this frame.
Component getGlassPane() :-
Returns the glassPane object for this frame.
Graphics getGraphics() :-
Creates a graphics context for this component.
JLayeredPane getLayeredPane() :-
Returns the layeredPane object for this frame.