Java Examples - Awt Frame Window - Owlcation - Education
Updated date:

Java Examples - Awt Frame Window

Author:

I am a software engineer. I have been working with C++, MFC, and .net technologies for 15 years. I like video games and reading books.

1. About AWT Frame

The AWT Frame is a Top-Level window which can host other child controls on it. A Frame can have a Title Window with Minimize, Maximize and Close buttons. The default layout of the AWT Frame is BorderLayout. In this example, we will create a Frame Window at run time with two labels in it.

2. Required Imports

First, we create a file called FrameWin.java and in this we will create our own Frame which is derived from java.awt.Frame. Below are the required import statements. We will see the usage of each class when the article progress.

import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

3. Set the Frame Title and Layout

First, we create a class called FrameWin and derive it from the AWT Frame. In our constructor, we take the Frame Title as a string and pass that to the base class constructor by calling super(). Next, we change the default BorderLayout to FlowLayout so that the Labels we will add get seated side-by-side. In addition, The SetLayout() function is used to change the default layout. The below depiction explains Title and FlowLayout.

Notepad vs FlowLayout

Notepad vs FlowLayout

We can map the notepad title to the Java Frame’s title. In the same way when can map the FlowLayout with how each typed letter appear in the Notepad. When we type, each character flow from left to right and when there is no room in the current line, the next letter appears in the leftmost edge of a next line of the screen. Now, imaging each letter as control occupying space in the Frame Window, we can get a picture how each control laid out in the Frame Window. Below is the code:

public class FrameWin 
extends Frame 
implements WindowListener {
    //Sample 01: Constructor
    public FrameWin(String FrameTitle){
        //Sample 02: Set Layout and Title
        super(FrameTitle);
        setLayout(new FlowLayout());

4. Add Labels to the Frame

As already told, a Frame window holds other child controls. The add() method is used to add child controls to the Frame. In our example, we are creating two label controls called L1 and L2. Then, we are adding that to the AWT Frame. Now, look at the depiction below:

Flow Layout and Labels

Flow Layout and Labels

Here, when we add to three Label Controls one-by-one, the third control automatically goes to the second line as there is no space for it in the first line. This kind of automatic arrangement is what called Flow Layout. Now, look at the code below which shows how we add the Label controls to Frame.

//Sample 03: Create Two Labels
Label L1 = new Label("Label 1");
Label L2 = new Label("Label 2");

//Sample 04: Add Label to the 
//          Frame Window
add(L1);
add(L2);

5. Setting the Size and Position of Frame

Note that we created the Frame when we made a call to the super(). Because, we called the base class constructor with string title and that constructed the Frame for us. Next, we added the labels and at this stage our Frame is ready.

We should set a position and size to our Frame. The size not only sets the width and height of the Frame but also helps in laying out the labels according the Flow Layout. On the other hand, the position tells where the Frame should appear. Have a look at the below depiction:

Size and Position of AWT Frame

Size and Position of AWT Frame

In the above picture, the black markers show the Frame width and height. The white markers show where the window will be positioned relative to the Top-Left corner of the desktop window. Now, have a look at the below code:

//Sample 05: Set Size of the Frame
setSize(400, 300);
setLocation(100,100);

6. Implementing WindowListener to Close the Frame

We derived our FrameWin class from java.awt.Frame and also claimed we will implement WindowListener. Java Framework calls the WindowListener functions when a window event takes place. For example, when a user minimize a window, Java calls windowIconified method. First, one need to tell the Frame that they are interested in responding to the window events by registering the Listener to it. We call the addWindowListener method and pass our FrameWin itself as a Listener since we will implement the WindowListener interface functions in it. Below is the code which adds the WindowListener to the Frame:

//Sample 06: Register with the Listener
addWindowListener(this);

And, here is the code which implements all the WindowListener interface functions.

//Sample 07: Implement the Listeners
public void windowOpened(WindowEvent e) {}
public void windowClosing(WindowEvent e) {
    this.dispose();
}
public void windowClosed(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}

Note we provided dummy implementation for all the function except the 'windowClosing'. Java AWT calls the 'windowClosing' function when a user clicks the ‘x’ button. We are calling the dispose method in it so that Frame window will get closed and Java AWT will release all associated memories. This ends the Frame window class definition. Now, we will create an instance out of it and display that.

7. Display the AWT Frame

We create a new java file called 'AwtFrame.java' and inside the static main we create the instance of our FrameWin. Note that we did all the work in the constructor itself and once the FrameWin is instantiated, it is ready to display. Hence, we call setVisible method to display the AWT Frame. Below is the code

//Sample 08: Create Frame and Display it
FrameWin fw = new FrameWin("My First Frame");
fw.setVisible(true);

Running the application will show the AWT Frame and its screenshot is given below:

AWT Frame Example

AWT Frame Example

8. Complete Code Listing

8.1 FrameWin.java

import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class FrameWin extends Frame 
implements WindowListener {
    //Sample 01: Constructor
    public FrameWin(String FrameTitle){
        //Sample 02: Set Layout and Title
        super(FrameTitle);
        setLayout(new FlowLayout());
        
        //Sample 03: Create Two Labels
        Label L1 = new Label("Label 1");
        Label L2 = new Label("Label 2");
        
        //Sample 04: Add Label to the 
        //          Frame Window
        add(L1);
        add(L2);
        
        //Sample 05: Set Size of the Frame
        setSize(400, 300);
        setLocation(100,100);
        
        //Sample 06: Register with the Listener
        addWindowListener(this);
        
    }

    //Sample 07: Implement the Listeners
    public void windowOpened(WindowEvent e) {}
    public void windowClosing(WindowEvent e) {
        this.dispose();
    }
    public void windowClosed(WindowEvent e) {}
    public void windowIconified(WindowEvent e) {}
    public void windowDeiconified(WindowEvent e) {}
    public void windowActivated(WindowEvent e) {}
    public void windowDeactivated(WindowEvent e) {}
}

AwtFrame.java

public class AwtFrame {
    public static void main(String[] args) {
        //Sample 08: Create Frame and Display it
        FrameWin fw = new FrameWin("My First Frame");
        fw.setVisible(true);
    }
}

© 2018 sirama

Related Articles