Creating a Frame : Swing Tutorials

This program shows you how to create a frame in Java Swing
Application. The frame
in java works like the main window where your components (controls) are added to
develop an application. In the Java Swing, top-level windows are represented by
the JFrame class. Java supports the look and
feel and decoration for the frame.
For creating java standalone application you
must provide GUI for a user. The most common way of creating a frame is, using single argument
constructor of the JFrame class. The argument of the constructor is the title of the window or frame.
Other user interface are added by constructing and adding it to the container one by one. The frame initially
are not visible and to make it visible the setVisible(true)
function is called passing the boolean value true. The close button of the frame by default performs the hide operation for
the JFrame. In this example we have changed this behavior to window close
operation by setting the setDefaultCloseOperation()
to EXIT_ON_CLOSE
value.
setSize (400, 400):
Above method sets the size of the frame or window to width (400) and height
(400) pixels.
setVisible(true):
Above method makes the window visible.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE):
Above code sets the operation of close operation to Exit the application using the System exit method.
Here is the code of the program :
import javax.swing.*;
public class Swing_Create_Frame{
public static void main(String[] args){
JFrame frame = new JFrame("Frame in Java Swing");
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
|
Download this example.

|
Current Comments
11 comments so far (post your own) View All Comments Latest 10 Comments:1 ) how to declare the variable static and acess them.
2) how to send data from one frame to another.
Posted by sanjana on Wednesday, 04.30.08 @ 16:06pm | #58216
Regarding Has Map.
Posted by Ramesh on Saturday, 03.1.08 @ 14:59pm | #50843
i have created the GUI using netbeans.But when project is run only the main window is displayed and none of the components in it are shown.How to connect Main.java and NewJFrame1.java in netbeans?
Posted by ramya on Friday, 02.29.08 @ 23:30pm | #50762
how to divide a frame in swings if so provide me some examples as soon as possible and tell me the way to know the key words of swings
Posted by uma on Saturday, 02.16.08 @ 17:00pm | #48583
I have created a main frame with 2 buttons, New and Close All. When New is clicked, a new frame is to be created titled Frame 1. Subsequent clicks of New will create additional frames, Frame 2, Frame 3, etc. When Close All is clicked in my main frame, all Frame n's are to close. I'm new to Java Swing and cannot seem to find an example on how to perform this in code? I see stuff on internal frames, but that is not what I am attempting to do. Can you help?
Posted by J. Beaumont on Wednesday, 05.30.07 @ 07:50am | #17703
How to use HashMap with Swings to retrieve data from database
Posted by Ashish Kumar on Friday, 04.20.07 @ 15:50pm | #14623
Hi,
P.Rajeswari!
I receive your problem for creating menus in the frame. Don’t worry; Pease, click the following link and get the creation process and description to menus or submenus in the frame.
Create menus and submenus
Vinod kumar
(RoseIndia.net)
Posted by vinod kumar on Friday, 04.13.07 @ 18:02pm | #14108
i need more inforamtion to create menu in hte frames plz help me sir
regards
raji
Posted by P.Rajeswari on Friday, 03.30.07 @ 10:52am | #12970
Hi Anand,
You can get the solution of your problem on the following link. Please click at:
http://www.roseindia.net/java/example/java/swing/RemoveTitleFrame.shtml
Thanks
Chandan Kumar
Roseindia Team Member
Posted by Chandan Kumar on Sunday, 03.18.07 @ 18:18pm | #12111
How to create a freme with out title bar
Posted by Anand kumar on Sunday, 03.18.07 @ 11:25am | #12078