Basics of AWT and Frame based applications in java


Java provide support for GUI in its core packages. AWT is one of the basic packages. It gives all you need to create a graphical software using frame windows. It provide buttons, scrollbars, text areas, text fields and many more components

Introduction to AWT

AWT stands for Abstract Window Toolkit. This relates to the branch of Java which is related mostly to the Graphics processing. You can create the GUI interfaces and create event based action on that interface.

Note: by interface here we don't mean Java Interface.

There are several classes in AWT package which will help you to do all the necessary steps before we start coding let us give you an overview of AWT important and commonly used classes.

Note: AWT is the base for the Java swings framework too. All the classes in swings extends from awt classes directly or indirectly.

Component: is the root class or say parent class of all the other classes in the window environment. The Component is made a parent because as its name suggests it satisfies the is-a relationship. That means Frame is a Component or say Container is a component. In this way.

Container: The container is sub class of the Component. Every graphical object will be hold in the container.

Panel: This is direct child of Container with no much difference. The applet is drawn on a panel. The panel serves tha base for the graphics drawing.

Window: this is nothing more than a subclass of container but it acts as a base for the GUI application just like the panel does.

Frame: This is going to be most important class for you as this helps to identify a graphical interface differently. It provide the boundaries and borders for the window based application.

Note: by window we don't mean MS windows operating system. You must remember the key feature of Java is platform Independence. The window is a graphical applet which displays the components on it.

Beside these main components there are several other classes Menu, MenuItem, Button, TextArea, TextField, Image, Label, ScrollPane, Font, Graphics, Robot, Event, Dimention, List, RadioButton, CheckBox etc. These are the components we see in our day to day life with GUIs.

Working With Window

The window we are talking about is the Framed Screen of any Application which helps us to do some operation to that software application. The general window contains the frame which provide it the close, minimize and restore buttons at the top. The re-sizable border which also separates it from other framed windows. You can also bind the event with the frame or window, using the event listener interface WindowListener.

In the first program we are only going to develop a framed window which will display a message. for this purpose we will use the Frame class as the superclass. Further code is as in following program.


package GUI;

import java.awt.*;
import java.awt.event.*;
import java.awt.event.WindowAdapter;

public class FirstFrame extends Frame{
    FirstFrame(){
        super("First GUI Frame");
        setSize(500, 500);
        setVisible(true);
        setBackground(Color.black);
        setForeground(Color.white);
        addWindowListener(new WindowAdapter() {
             public void windowClosing(WindowEvent we){
                 System.exit(0);
             }
        });
    }
    
    public void paint(Graphics g){
        g.drawString("something", 50,50);
    }
    
    public static void main(String[] args){
        new FirstFrame();
    }
}

So here we are with our first ever hello world program in graphical environment. So lets us see the result and then we can worry about the working of the code.

First Frame

So as you see this is a framed window of dimension 500x500. the title as we have set is First GUI Frame. the frame class poses two constructor, Frame() and Frame(String s). The second one takes the string type of parameter and set it as the title of frame. As in this case we have extended the Frame class in FirstFrame class we used super() constructor to do the work for us. after that we have set the size of the frame window. By default the frame is re-sizable but you can make it non re-sizable by using setResizable(false);.

Note: if your frame look different on the title bar and buttons, don't panic there is nothing wrong probably you are using some other operating system(we are using ubuntu). The look of frame depends on the OS usually.

There is a whole lot of methods provided with the frame class which you can use to customize the look and feel of your graphical application. For instance we have colored the frame background black and the foreground as white.

What you need to understand is that, the frame does not work when you close it using the close button because there is no action bind to that button just yet, so we bind a window listener to the frame and we set its closing event to exit the execution. And yet again the paint method is used to draw on the frame.

This is the first ever application that we used to create a frame next time we will be creating a frame in an applet and we will try and put some buttons and text field on that frame. But do remember you can not add the frame to applet because it is a panel which goes in a different hierarchy, when we said frame in an applet we meant we will initialize the from an applet. If you have practiced this tutorial you can move on to Working With Frames in Java.