Running a method when closing the program?

后端 未结 6 1239
無奈伤痛
無奈伤痛 2020-12-03 14:22

I need to execute a method, (a method which creates a file), when I exit my program, how would I do this?

6条回答
  •  旧时难觅i
    2020-12-03 15:04

    addWindowListener is better solution:

    frame.addWindowListener(new WindowAdapter()
    {
        public void windowClosing(WindowEvent we)
        {
          // run methods before closing  
    
    try {
    
                                    Runtime.getRuntime().exec("taskkill /f /im java.exe");
    
                                } catch (IOException e4) {
                                    // TODO Auto-generated catch block
                                    e4.printStackTrace();
                                }
    
        }
    });
    

提交回复
热议问题