Java Swing Tutorials

adplus-dvertising
Creating a Frame
previous Home Next

In this page of the tutorials we are try to create a frame this frame work as like a window where we can add our controls for developing the 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.

In this example of the tutorials we are going to creating java standalone application for doing this we must provide GUI for a user. The most popular way to creating a frame is that, we can use single argument constructor of the JFrame class. The constructor's argument is the title of 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.

Method Description

  1. setSize (400, 400): setSize() method is used to set the size of the frame or window to width (400) and height (400) pixels.
  2. setVisible(true): This method is used to make the window visible.
  3. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE): This method is used to set the operation of close to Exit the application using the System exit method.

Example

package r4r;
import javax.swing.*;
public class createframe
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Frame Creation Done");
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
previous Home Next