Home Java Example Java Swing Creating a Frame
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Creating a Frame

Advertisement
This program shows you how to create a frame in Java Swing Application.

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(400400);
  frame.setVisible(true);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

Download this example.

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 14, 2007

Ask Questions?    Discuss: Creating a Frame   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Rowan
June 22, 2011
Other way to write a frame

Hi, I found out that this code described, did not work. Instead, you can use: import javax.swing.JFrame; class ShowAFrame { public static void main(String args[]) { JFrame myFrame = new JFrame(); String myTitle = "MyTitle"; myFrame.setTitle("Blank Frame"); myFrame.setSize(200, 200); myFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); myFrame.setVisible(true); } } Greetings from holland :)
debasish deb
December 19, 2011
network monitoring tool

i want to develop an application on java using netbeans7.0 software ... motive is: 1. 1st of all developing a GUI . 2. giving buttons. so should i proceed in swing GUI forms and MDI application? i m not sure how to proceed .... plz help me. which way i should proceed.... plz advice..
Supandi
December 23, 2011
How to make Maximize Frame?

hi,,, programmer java.. how to making the maximize frame size...??
s.vinodhini
January 7, 2012
doubt

how can we create a wireless connection using java
nilesh savaliya
January 15, 2013
how to create a frame in a java

plz help me how to create a frame in a java give the example with solution help
vikram prajpait
January 16, 2013
java

learn java
mie an
February 13, 2012
jframe

good program ,even though i cannot really understand
samina mulani
February 18, 2012
Jframe

Its simple n good eg to start...
venkat
April 13, 2012
java

OK. I WILL TRY TO DOO THIS THANKS FOR U R HELP
festus
June 9, 2012
how to become a good java programmer

Please i need someone to be my tutor on java. Please
suresh
July 12, 2012
studies

java work into jobs company site
ajay
July 23, 2012
how to learn java programs?

java programming language is easy or hard can you explain me with examples in my email id?
fadlhee
August 9, 2012
you

nice code but too easy!
mohsen
August 16, 2012
how can i debug this code

hi. my name is mohsen.i'm from iran.i want to study java programming.i'm new.how can i debug this code.it dosen't work(i use NetBeans IDE 7.1.2) tanks.i'm wating
mohsen
August 16, 2012
i need help so

hi. my name is mohsen.i'm from iran.i want to study java programming.i'm new. how can i debug this code.it dosen't work(i use NetBeans IDE 7.1.2) tanks
mohsen
August 16, 2012
i need help so

hi. my name is mohsen.i'm from iran.i want to study java programming.i'm new. how can i debug this code.it dosen't work(i use NetBeans IDE 7.1.2) tanks
pedro
August 23, 2012
me myself and i

i dont like your freaking codes! its just to easy! Even 4 years old child and below can also do that!
vijayakumar.s
September 24, 2012
java

i can have more thing in frames
TOPIK
June 20, 2013
Tiw tiw tiw

How to Make Frames (Main Windows)
DMCA.com