Java Program to Close the Frame Using an Anonymous Class

«
»

This is a Java program to close the frame using an anonymous class.

Problem Description

We have to write a program in Java such that it closes a frame on click of an exit button using an anonymous class.

Expected Input and Output

For closing of frame using an anonymous class, we can have the following 2 different sets of input and output.

advertisement

1. To test the opening of the frame: When the frame is opened after creation.

For example:

If the frame is created and opening, that is when the program is first executed
then the expected output is "Frame Opened Successfully"

2. To test the closing of the frame: When the frame is closed.

advertisement

For example:

If the frame is closed, that is on click of the "Exit" button on frame
then the expected output is "Frame Closed Successfully"
Problem Solution

1. Create a frame, an exit button and object of the class.
2. Define a constructor of the class which checks the actions on the exit button and disposes the frame if the button is clicked.

Program/Source Code

Here is source code of the Java Program to handle the keyboard events. The program is successfully compiled and tested using BlueJ on Windows 10 and javac compiler on Fedora 30.

advertisement
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. class Close_Frame
  5. {
  6.     static JFrame frame;
  7.     static JButton exit;
  8.     //Constructor
  9.     Close_Frame()
  10.     {
  11.         exit.addActionListener(new ActionListener(){
  12.             @Override
  13.             public void actionPerformed(ActionEvent e)
  14.             {
  15.                 frame.dispose();
  16.                 System.out.println("Frame Closed Successfully");
  17.             }
  18.         });       
  19.     }
  20.     //Driver Function
  21.     public static void main(String args[])
  22.     {
  23.         //Create a frame
  24.         frame=new JFrame("Close Frame Using Anonymous Class");
  25.         frame.setSize(500,500);
  26.         frame.setBackground(Color.white);
  27.         frame.setLayout(null);
  28.         //Create an exit button
  29.         exit=new JButton("Exit");
  30.         exit.setBounds(220,235,60,30);
  31.         frame.add(exit);
  32.         //Create an object of the class
  33.         Close_Frame obj=new Close_Frame();
  34.         //Display frame
  35.         frame.setVisible(true);
  36.         System.out.println("Frame Opened Successfully");
  37.     }
  38. }
Program Explanation

1. Create a frame with background color & specific size. frame.setBackground(Color.white); is used to create the frame background color as white. frame.setSize(500,500); is used to set the width and height of the frame.
2. Create an exit button with setBounds(int x, int y, int width, int height), where ‘x’ & ‘y’ is used to set the position of top-left corner. ‘int width’ & ‘int height’ is used to set the width and height of the exit button.
3. Here the values of set bounds are setBounds(220,235,60,30), where ‘220’ & ‘235’ is used to set the position of button from top and left side of the frame. ’60’ & ’30’ is used to set the width and height of the exit button.
4. Create an object of the class with a name Close_Frame. When the object is created, the constructor is called and it checks the event of the button.
5. When the exit button is clicked, the constructor closes the frame and displays suitable message.

Runtime Test Cases

Here’s the run time test cases for closing of frame using an anonymous class.

Test case 1 – Here’s the runtime output of the frame opening. The program will create and displays the frame.
java-program-close-frame-using-anonymous-class-frame-opened

advertisement

Test case 2 – Here’s the runtime output of the frame closing. When the exit button is clicked, it closes the frame and displays suitable message.
java-program-close-frame-using-anonymous-class-frame-closed

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn