Pages

Close JFrame when mouse click release in JFrame

Source code below will create a JFrame. After that, click in the JFrame, hold for a few seconds, and after that release your mouse click. The JFrame that created in this program will be close and this application will terminate.

********************************************************************
COMPLETE SOURCE CODE FOR : CloseJFrameWhenMouseReleased.java
********************************************************************


import javax.swing.JFrame;

import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class CloseJFrameWhenMouseReleased
{
public static void main(String[]args)
{
//Create mouse listener that will listen when mouse released in JFrame
MouseListener ml=new MouseAdapter()
{
public void mouseReleased(MouseEvent event)
{
//Put JFrame close code here
System.exit(0);
}
};

//Create JFrame with title ( CLICK ON ME,HOLD FOR A FEW SECOND,RELEASE YOUR CLICK )
JFrame frame=new JFrame("CLICK ON ME,HOLD FOR A FEW SECOND,RELEASE YOUR CLICK");

//Add mouse listener to JFrame
frame.addMouseListener(ml);

//Set default close operation to JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size to :
//Width : 700 pixels
//Height : 400 pixels
frame.setSize(700,400);

//Make JFrame visible. So we can see it.
frame.setVisible(true);
}
}


********************************************************************
JUST COMPILE AND EXECUTE IT
********************************************************************
Related Posts Plugin for WordPress, Blogger...