import javax.swing.*;public class Test extends JFrame { public Test() { pack(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { System.out.println("Do something before closing the frame!"); } }); setVisible(true); }
public static void main(String[] args) { new Test(); } }
给你写个简单的例子吧。import java.awt.Dimension; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import javax.swing.JFrame; public class Test extends JFrame implements WindowListener{ public Test(){ this.setSize(new Dimension(100,100)); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); this.addWindowListener(this); } @Override public void windowActivated(WindowEvent e) { } @Override public void windowClosed(WindowEvent e) { System.out.println("asdf"); } @Override public void windowClosing(WindowEvent e) { System.out.println("asdf"); } @Override public void windowDeactivated(WindowEvent e) { } public void windowDeiconified(WindowEvent e) { } @Override public void windowIconified(WindowEvent e) { } @Override public void windowOpened(WindowEvent e) { }
public static void main(String[] arg){ new Test(); } }
{
public Test()
{
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Runtime.getRuntime().addShutdownHook(new Thread()
{
public void run()
{
System.out.println("Do something before closing the frame!");
}
});
setVisible(true);
}
public static void main(String[] args)
{
new Test();
}
}
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
public class Test extends JFrame implements WindowListener{
public Test(){
this.setSize(new Dimension(100,100));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.addWindowListener(this);
}
@Override
public void windowActivated(WindowEvent e) {
}
@Override
public void windowClosed(WindowEvent e) {
System.out.println("asdf");
}
@Override
public void windowClosing(WindowEvent e) {
System.out.println("asdf");
}
@Override
public void windowDeactivated(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
@Override
public void windowIconified(WindowEvent e) {
}
@Override
public void windowOpened(WindowEvent e) {
}
public static void main(String[] arg){
new Test();
}
}