Table of Contents
a) Introduction
b) Source Code
c) Description of Source Code
d) Output
Introduction
To begin with java, We all know that java can build all types of the application whether it is Web Based, Android Based and Desktop Based Applications. It is the one of the oldest programming languages and also the high level language.
Java with AWT Frame needs to be handled with Event Delegation Model commonly known as event handling. Here, I use WindowAdapter to do so.
Let's see the source code and decode it line by line;
Source Code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author AnkitPC
*/
import java.awt.*;
import java.awt.event.*;
public class AWTFrameDemo extends WindowAdapter {
Frame f;
AWTFrameDemo(){
f=new Frame();
f.setTitle("Welcome");
f.setSize(400,300);
f.addWindowListener(this);
f.setVisible(true);
}
public static void main(String[] args) {
new AWTFrameDemo();
}
public void windowClosing(WindoIvent e){
f.dispose();
}
}
Starting from the top:
We should import the required packages to run the program. This is the inbuilt packages in java. And importing a package is always must be the first statement of the program in java.
import java.awt.*;
import java.awt.event.*;
We have to import these two packages because in-order to use AWT components (i.e Frame , Button etc) I need to import java.awt package. And as I have said already that I need to handle the event to create its frame so I import the java.awt.event package.
[Note: These to packages look same because of java.awt but these packages are different and should be imported.]
Now I have the main class called AWTFrameDemo which extends the WindowsAdapter. As I all know that I can implement WindowListener also in this program but there is always a advantage of using Adapter class over Listener interface. It is because that I only need to override the method that is needed.
Inside the main class, I have declared a variable Frame f1. After that a constructor is created. It is not compulsory to make the constructor as you can make the method also. So it is all up to you to create a constructor or to create the method. The object of Frame is created, and title is provided with the method called setTitle(String title). Until and Unless you provide the size of the frame you will not see it. So set a size of frame using setSize(int width,int height) method. And you must add the windowListener to the frame in-order to make it closeable. At last don’t forget to set the frame to visible which is done using the setVisible() method. By default the value in the setVisible() method will be ‘false’ so make it ‘true’. This method takes Boolean as value.
Now comes the main method. Inside it we just have to call the constructor and it will do the rest of the work. But till now you will se the error in the program because we have not handled the event.
Therefore windowClosing(WindowEvent e) is the method inside the WindowListener/WindowAdapter that is overrided and dispose() method is called to close the frame.
See the Output;
And that’s how we can make the AWT frame closable.
Post a Comment
0 Comments