Create a SWING GUI Frame and AWTÂ (Abstract Window Toolkit)Â Frame which is Closable
SWING GUI Frame Demo
Table of Contents
a) Introduction
b) Source Code
c) Description of Code
d) Output
Introduction
Working with SWING
Frame means building the Desktop-Based GUI (Graphical
User Interface) Applications. In this series I am going to tech you
about the some basic about how to create a SWING Frame and also to make it
closable. In this Frame I am not going to add any component but I will make you
to be able to create the Frame using SWING.
[Note: Frame is the container in which different
components are added.]
Now let’s have a look into the source code, and then
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 javax.swing.*;
public
class SwingFrameDemo {
  JFrame
f;
  SwingFrameDemo(){
//constructor
   f=new
JFrame("Swing Frame");
   f.setSize(400,300);
   f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   f.setVisible(true);
 }
 //main
method
  public
static void main(String[] args) {
   //call
constructor
   new
SwingFrameDemo();
  }
}
The first line of this code is importing
of package. It is necessary to import the package i.e javax.swing to use the
components of that package. For a SWING GUI (Graphical User Interface) to make
closable we does not need to handle the event. It is done just by using the
method.
I have named the main class as SwingFrameDemo and declared a JFrame global variable. I have created a
constructor but it is not compulsory to use the constructor. You can make a
method also and it’s up to you.
[ Note: Making a constructor means you
should not create an object to access the constructor. But if you use method
instead of the constructor than you have to create the object and it is
compulsory. ]
[ Note: You can use setTitle(String title) method to give the title
to the frame. ]
setSize(int
width,int height)
method is used to provide the size of the frame. Without this method your frame
may look like this.
I said already that you just need to call the method
to make the SWING GUI closable rather than
handling the event. The method is setDefaultCloseOperation().
In this method you can pass JFrame.EXIT_ON_CLOSE
or JFrame.DISPOSE_ON_CLOSE. These two works
same for the single frame but not for multiple frame. We will learn it in
further tutorials.
By default the frame visibility is none so you have
to manually set its visibility. You can set is visibility by using the setVisible() method and inside it pass ‘true’ as
it takes Boolean value.
Now at last we
have the main method. Inside it I have called the constructor which will do the
work. Let us see the output now:
Note: Components are not added in the frame so it is
empty.
AWTÂ (Abstract Window Toolkit)Â Frame
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.