INTRODUCTION OF JFRAME

Introduction of JFrame


JFrame

  • JFrame objects store several objects including a Container object known as the content pane. To add a component to a JFrame, add it to the content pane.

JFrame Features

  • JIt’s a window with title, border, (optional) menu bar and user-specified components.

  • It can be moved, resized, iconified.

  • It is not a subclass of JComponent.

  • Delegates responsibility of managing user-specified components to a content pane, an instance of JPanel.

Centering JFrame’s

    By default, a Jframe is displayed in the upper-left corner of the screen. To display a frame at a specified location, you can use the setLocation(x, y) method in the JFrame class. This method places the upper-left corner of a frame at location (x, y).The Swing API keeps improving with abstractions such as the setDefaultCloseOperation methodfor the JFrame.

Crating a JFrame Window

  • Step 1: Construct an object of the JFrame class.

  • Step 2: Set the size of the Jframe.

  • Step 3: Set the title of the Jframe to appear in the title bar (title bar will be blank if no title is set).

  • Step 4: Set the default close operation. When the user clicks the close button, the program stops running.

  • Step 5: Make the Jframe visible.

How to position JFrame on Screen?

    frame.setLocationRelativeTo( null );
 
            
                package com.sdj;
                import java.awt.FlowLayout;
                import javax.swing.JFrame;
                import javax.swing.JLabel;
                public class SdjFrame extends JFrame
                {
                    public static void main(String[] args)
                    {
                        SdjFrame frame=new  SdjFrame();
                        frame.setSize(400, 400);
                        frame.setVisible(true);
                        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    }
                    JLabel lb;
                    public  SdjFrame()
                    {
                        setLayout(new FlowLayout());
                        lb=new JLabel("Welcome in SDJ");
                        lb.setBounds(200, 50, 100, 25); /*  set the position(cordinates) of label on frame */
                        setLayout(null);
                        add(lb); /* add the label to frame */
                    }
                } 
            

Output:

Naming Convention
Swing-Welcome SDJ