Wednesday, October 14, 2009

Java Source Code – Slowly Closing the Window of a JFrame – Tricks

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;


public class SlowClose {
public SlowClose() {

final JFrame jFrame = new JFrame();
jFrame.setSize(500,350);
jFrame.setVisible(true);
jFrame.setResizable(false);
jFrame.setLocationRelativeTo(null);
jFrame.setTitle("SlowClose");


jFrame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
int x = 500;
int y = 350;
int d = 10;
while(x > 100 && y > 50){
x = x-d;
y = y-d;
jFrame.setSize(x,y);
System.out.println("X = " + x + "Y = " + y);
try{
Thread.sleep(10000);
}catch(InterruptedException ie){
ie.printStackTrace();
}

if(x <= 100 y <= 50){
System.exit(0);
}
}
}
});
}

public static void main(String args[]) {
SlowClose slowClose = new SlowClose();
}

}

No comments:

Total Pageviews