Java JFrame
The javax.swing.JFrame class is a type of container which inherits the java.awt.Frame
class. JFrame works like the main window where components like labels, buttons,
textfields are added to create a GUI.
Unlike Frame, JFrame has the option to hide or close the window with the help of
setDefaultCloseOperation(int) method.
Nested Class
Modifier and Class Description
Type
protected JFrame.AccessibleJFram This class implements accessibility
class e support for the JFrame class.
Fields
Modifier and Type Field Description
protected accessibleContext The accessible context property.
AccessibleContext
static int EXIT_ON_CLOSE The exit application default window
close operation.
protected rootPane The JRootPane instance that
JRootPane manages the contentPane and
optional menuBar for this frame, as
well as the glassPane.
protected boolean rootPaneCheckingEnable If true then calls to add and
d setLayout will be forwarded to the
contentPane.
Constructors
Constructor Description
JFrame() It constructs a new frame that is initially
invisible.
JFrame(GraphicsConfiguration gc) It creates a Frame in the specified
GraphicsConfiguration of a screen device and a
blank title.
JFrame(String title) It creates a new, initially invisible Frame with
the specified title.
JFrame(String title, It creates a JFrame with the specified title and
GraphicsConfiguration gc) the specified GraphicsConfiguration of a screen
device.
Useful Methods
Modifier and Method Description
Type
protected void addImpl(Component comp, Object Adds the specified
constraints, int index) child Component.
protected createRootPane() Called by the
JRootPane constructor methods
to create the default
rootPane.
protected void frameInit() Called by the
constructors to init
the JFrame properly.
void setContentPane(Containe contentPane) It sets the
contentPane property
static void setDefaultLookAndFeelDecorated(boolean Provides a hint as to
defaultLookAndFeelDecorated) 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.
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 It sets the
layeredPane) layeredPane
property.
JRootPane getRootPane() It returns the
rootPane object for
this frame.
TransferHandler getTransferHandler() It gets the
transferHandler
property.
JFrame Example
1. import java.awt.FlowLayout;
2. import javax.swing.JButton;
3. import javax.swing.JFrame;
4. import javax.swing.JLabel;
5. import javax.swing.Jpanel;
6. public class JFrameExample {
7. public static void main(String s[]) {
8. JFrame frame = new JFrame("JFrame Example");
9. JPanel panel = new JPanel();
10. panel.setLayout(new FlowLayout());
11. JLabel label = new JLabel("JFrame By Example");
12. JButton button = new JButton();
13. button.setText("Button");
14. panel.add(label);
15. panel.add(button);
16. frame.add(panel);
17. frame.setSize(200, 300);
18. frame.setLocationRelativeTo(null);
19. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
20. frame.setVisible(true);
21. }
22. }
Output