Java Swing Tutorial - JFrame
JFrame is a top-level container that every GUI application has.To create a JFrame application first, you need.
- Create a JFrame object.
- 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(); }
}
]