setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//这个方法,我想在调用这个方法之前需要做一些事情,请问怎么搞?

解决方案 »

  1.   

    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();
    }
    }
      

  2.   

    给你写个简单的例子吧。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();
    }
    }
      

  3.   

    也就是说,当你点击X图片的时候,会触发WindowListener的关闭事件。你需要做一些事情,可以在这个事件中作。