Create a Frame in Java

In this section, you will learn how to create a frame in java awt.

Create a Frame in Java

In this section, you will learn how to create a frame in java awt.

Create a Frame in Java

Create a Frame in Java



This program shows you how to create a frame in java AWT package. The frame in java works like the main window where your components (controls) are added to develop a application. In the Java AWT, top-level windows are represented by the Frame class. Java supports the look and feel and decoration for the frame. For creating java standalone application you must provide GUI to the user.

The most common method of creating a frame is by using single argument constructor of the Frame class that contains the single string argument which is the title of the window or frame. Then you can add user interface by constructing and adding different components to the container one by one.

In this program we are constructing a label to display "Welcome to Tutorial." message on the frame. The center alignment of the label has been defined by the Label.CENTER. The frame initially invisible, so after creating the frame it need to visualize the frame by setVisible(true) method.

This method has been used to add the label to the frame. Method add() adds a component to it's container.

setSize (width, height):
This is the method of the Frame class that sets the size of the frame or window. This method takes two arguments width (int), height (int).

This is also a method of the Frame class sets the visibility of the frame. The frame will be invisible if you pass the boolean value false otherwise frame will be visible.

Here is the code of the program : 

import java.awt.*;

public class AwtFrame{
public static void main(String[] args){
Frame frm = new Frame("Java AWT Frame");
Label lbl = new Label("Welcome to Tutorial.",Label.CENTER);

Download this example.

To run this example code first compile it with following command:


and to run type following command:

java AwtFrame

This program will display following screen:

AWT Frame Example

In this tutorial you learned to create simple Frame and Label in Java AWT.

Here are more examples: