JFrame在关闭时拦截出口以保存数据 [英] JFrame intercept exit on close to save data

查看:114
本文介绍了JFrame在关闭时拦截出口以保存数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个窗口,并希望在应用程序关闭之前使用windowStateChanged方法拦截出口以保存数据.但是,关闭之前似乎没有保存数据.我该如何纠正?

请参见下面的代码:

public class InventoryMainFrame extends JFrame implements WindowStateListener{
    //set up the main window - instantiate the application
  private InventoryInterface inventoryInterface;    //panel that contains menu choices and buttons

  public InventoryMainFrame(){   //main window        
      setTitle("Inventory System");
      setSize (500,500);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLayout(new BorderLayout());
      //setLocationRelativeTo(null);    //center window on the screen           
      inventoryInterface = new InventoryInterface();    //set up the panel that contains menu choices and buttons
      add(inventoryInterface.getMainPane());         //add that panel to this window                   
      pack();
      setVisible(true); 

      //display window on the screen           
  }

public static void main(String[] args) {        
    //sets up front end of inventory system , instantiate the application
    InventoryMainFrame aMainWindow = new InventoryMainFrame( );
}

@Override
public void windowStateChanged(WindowEvent w) {
    //intercept the window close event so that data can be saved to disk at this point
    if (w.getNewState()==WindowEvent.WINDOW_CLOSED){
        //save the index file
        try{
          inventoryInterface.getInventory().saveIndexToFile();
          System.out.println("saving");
          dispose();  //dispose the frame
         }
        catch(IOException io){
            JOptionPane.showMessageDialog(null,io.getMessage());
        }
    }       
}

}

解决方案

您应该尝试注册如何编写窗口侦听器.

I created a window and want to intercept the exit with the method windowStateChanged to save the data before the application closes. However, it doesn't appear to be saving the data before it closes. How can I correct this?

see code below:

public class InventoryMainFrame extends JFrame implements WindowStateListener{
    //set up the main window - instantiate the application
  private InventoryInterface inventoryInterface;    //panel that contains menu choices and buttons

  public InventoryMainFrame(){   //main window        
      setTitle("Inventory System");
      setSize (500,500);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLayout(new BorderLayout());
      //setLocationRelativeTo(null);    //center window on the screen           
      inventoryInterface = new InventoryInterface();    //set up the panel that contains menu choices and buttons
      add(inventoryInterface.getMainPane());         //add that panel to this window                   
      pack();
      setVisible(true); 

      //display window on the screen           
  }

public static void main(String[] args) {        
    //sets up front end of inventory system , instantiate the application
    InventoryMainFrame aMainWindow = new InventoryMainFrame( );
}

@Override
public void windowStateChanged(WindowEvent w) {
    //intercept the window close event so that data can be saved to disk at this point
    if (w.getNewState()==WindowEvent.WINDOW_CLOSED){
        //save the index file
        try{
          inventoryInterface.getInventory().saveIndexToFile();
          System.out.println("saving");
          dispose();  //dispose the frame
         }
        catch(IOException io){
            JOptionPane.showMessageDialog(null,io.getMessage());
        }
    }       
}

}

解决方案

You should try registering a WindowAdapter and override its windowClosing method. For more information, see How to Write Window Listeners.

这篇关于JFrame在关闭时拦截出口以保存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆