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.
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.
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:
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:
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.
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:
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:
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:
And, here is the code which implements all the WindowListener interface functions.
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
Running the application will show the AWT Frame and its screenshot is given below:
8. Complete Code Listing
© 2018 sirama