Java Swing Tutorial - JFrame

Java Swing Tutorial - JFrame

JFrame is a top-level container that every GUI application has.To create a JFrame application first, you need.

  1. Create a JFrame object.
  2. Make the object visible.

How to create a JFrame Object

[JFrame frame=new JFrame("Simple JFrame");]

JFrame object takes a String that is displayed as the JFrame title.JFrame class is in javax.swing package.

Making JFrame visible

By default, a JFrame is not visible.To make JFrame visible we have to call setVisible(true) method i.e
[
JFrame frame=new JFrame("Simple JFrame");
        frame.setVisible(true);
]


Exiting a Swing application

We can do hat should happen to a swing application when a JFrame window is closed.JFrame constants contained in package javax.swing.WindowConstants interface.
These constants are:
  • DO_NOTHING_ON_CLOSE-nothing happens when the JFrame window is closed.
  • HIDE_ON_CLOSE-JFrame window hides itself when the window is closed.
  • DISPOSE_ON_CLOSE-JFrame window hides and get disposed  releasing all the resources it               was holding
  • EXIT_ON_CLOSE-exits application.
Example
[
import javax.swing.JFrame;

public class JFrameExample {
    JFrameExample()
    {
        JFrame frame=new JFrame("Simple JFrame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }

}
]

JFrame Size and Location

Once we have created a JFrame, we need to set JFrame size i.e., Width and height and its location of the screen i.e., x and y-axis.

The two methods that are used are setSize(int w,int h) and setLocation(x ,y);
Example.
[

import javax.swing.JFrame;

public class JFrameExample {
    JFrameExample()
    {
        JFrame frame=new JFrame("Simple JFrame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.setLocation(20, 20);

        frame.setVisible(true);

    }
}
]
To position JFrame window at the center of the screen we used setLocationRelativeTo(null)  method.
We can also use setBounds method to set width, height, x  and  y to set JFrame position.
[frame.setBounds(20, 20, 400, 300);]

Adding Swing  components to JFrame

To add swing components to JFrame we have to get the reference of contentpane.

Example.

[
//create a jframe window
JFrame frame=new JFrame("Simple JFrame");
//call reference to container 
        Container container=frame.getContentPane();
//create a JButton
        JButton button=new JButton("Save");
//call add method
        container.add(button);
     
]


Full code

[
import javax.swing.JFrame;
import java.awt.Container;
import javax.swing.JButton;

public class JFrameExample {
    JFrameExample()
    {
        JFrame frame=new JFrame("Simple JFrame");
        Container container=frame.getContentPane();
        JButton button=new JButton("Save");

        container.add(button);


        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.setLocation(20, 20);

        frame.setVisible(true);

    }
    public static void main(String args[])
    {

        new JFrameExample();
    }

}
]

JFrame Pack Method

pack() is a JFrame that is used to determine all JFrame components and determines their sizes.If you use pack method, there is no need to call setBounds() method.
pack() method is used with setLocation(x y ) method.

Example
[

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Container;

public class JavaPackMethodExample  extends JFrame{
    JavaPackMethodExample()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Add a close button        JButton closeButton = new JButton("Close");
        Container contentPane = this.getContentPane();
        contentPane.add(closeButton);

        // Calculates and sets appropriate size for the frame        pack();

        setVisible(true);

    }
    public static void main(String args[])
    {
        new JavaPackMethodExample();
    }
}
]

See also:


JFrame Window Events







COMMENTS

Name

android arrays cnna1 graphics java java control statements java.swing JButton JFrame JLabel JTextField laravel node.js OPP questions swing technology
false
ltr
item
Code WHIZZ: Java Swing Tutorial - JFrame
Java Swing Tutorial - JFrame
Java Swing Tutorial - JFrame
Code WHIZZ
https://code-whizz.blogspot.com/2017/03/java-swing-tutorial-jframe.html
https://code-whizz.blogspot.com/
http://code-whizz.blogspot.com/
http://code-whizz.blogspot.com/2017/03/java-swing-tutorial-jframe.html
true
5534598864497432585
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy