Pages

Close JWindow using mouse click on it

Source code below will show you how to create a JWindow that can terminate when we click on it.

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


import javax.swing.JWindow;
import javax.swing.JLabel;

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

import java.awt.FlowLayout;

public class ClosingJWindowUsingMouseClick
{
public static void main(String[]args)
{
//Create a JWindow
JWindow window=new JWindow();

//Create a JLabel that we want to add to JWindow
JLabel label=new JLabel("Click me to exit");

//Set layout for JWindow
window.setLayout(new FlowLayout());

//Add label to JWindow
window.add(label);

//Create Mouse Listener that listen when someone click on that JWindow
MouseListener ml=new MouseAdapter()
{
public void mousePressed(MouseEvent evt)
{
//Exit application
System.exit(0);
}
};

//Add Mouse Listener to JWindow
window.addMouseListener(ml);

//Set JWindow size with width=400 pixels and height=400 pixels
window.setSize(400,400);

//Make JWindow visible
window.setVisible(true);
}
}


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