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.

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
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.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
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.

  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.

advertisement
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

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

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.