JFrame is a class in Java’s Swing library, which is used to create a window where graphical user interface (GUI) components like buttons, labels, and text fields can be added.
JFrame(): Creates a new, initially invisible frame with no title.JFrame(String title): Creates a new, initially invisible frame with the specified title.
package jframes;
import javax.swing.JFrame;
public class MyFrame extends JFrame {
public MyFrame() {
// Set the title of the JFrame
setTitle("My First JFrame");
/*
* Set the default close operation so the application
exits when the frame is closed
*/
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Set the size of the JFrame
setSize(400, 300);
//Set the x,y location of a Frame
setLocation(100, 100);
// Make the JFrame visible
setVisible(true);
}
public static void main(String[] args) {
// Create an instance of MyFrame
new MyFrame();
}
}

The above program can be written like the following code snippets
package jframes;
import javax.swing.JFrame;
public class MyFrame {
public static void main(String[] args) {
//Create an object of JFrame
JFrame jFrame = new JFrame();
// Set the title of the JFrame
jFrame.setTitle("My First JFrame");
/*
* Set the default close operation so the application
exits when the frame is closed
*/
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Set the size of the JFrame
jFrame.setSize(400, 300);
//Set the x,y location of a Frame
jFrame.setLocation(100, 100);
// Make the JFrame visible
jFrame.setVisible(true);
}
}
package jframes;
import javax.swing.JFrame;
public class MyFrame {
public static void createJFrame() {
//Create an object of JFrame
JFrame jFrame = new JFrame();
// Set the title of the JFrame
jFrame.setTitle("My First JFrame");
/*
* Set the default close operation so the application
exits when the frame is closed
*/
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Set the size of the JFrame
jFrame.setSize(400, 300);
//Set the x,y location of a Frame
jFrame.setLocation(100, 100);
// Make the JFrame visible
jFrame.setVisible(true);
}
public static void main(String[] args) {
createJFrame();
}
}
package jframes;
import javax.swing.JFrame;
class MyFrame {
public void createGUI() {
//Create an object of JFrame
JFrame jFrame = new JFrame();
// Set the title of the JFrame
jFrame.setTitle("My First JFrame");
/*
* Set the default close operation so the application
exits when the frame is closed
*/
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Set the size of the JFrame
jFrame.setSize(400, 300);
//Set the x,y location of a Frame
jFrame.setLocation(100, 100);
// Make the JFrame visible
jFrame.setVisible(true);
}
}
public class MyFrameMain {
public static void main(String[] args) {
MyFrame myFrame = new MyFrame();
myFrame.createGUI();
}
}
| Method Name | Description |
|---|---|
void setTitle(String title) |
Sets the title for the frame. |
String getTitle() |
Gets the title of the frame. |
void setSize(int width, int height) |
Sets the size of the frame. |
void setSize(Dimension d) |
Sets the size of the frame using a Dimension object. |
void setVisible(boolean b) |
Makes the frame visible or invisible. |
void setDefaultCloseOperation(int operation) |
Sets the operation that will happen by default when the user initiates a “close” on this frame. |
void setLocation(int x, int y) |
Sets the location of the frame relative to the screen. |
void setLocation(Point p) |
Sets the location of the frame using a Point object. |
void setBounds(int x, int y, int width, int height) |
Sets the size and location of the frame. |
void add(Component comp) |
Adds a component to the frame. |
void remove(Component comp) |
Removes the specified component from the frame. |
void setResizable(boolean resizable) |
Sets whether this frame is resizable by the user. |
boolean isResizable() |
Returns whether this frame is resizable by the user. |
void setIconImage(Image image) |
Sets the image to be displayed as the icon for this frame. |
Image getIconImage() |
Returns the icon image for this frame. |
You must be logged in to submit a review.