3
응용 프로그램을 닫기 전에 데이터를 저장하기 위해 창을 만들고 windowStateChanged 메서드를 사용하여 끝내기를 가로 채려고합니다. 그러나 닫히기 전에 데이터를 저장하는 것으로 나타나지 않습니다. 이 문제를 어떻게 해결할 수 있습니까? 아래JFrame은 닫을 때 데이터를 저장하기 위해 닫습니다.
참조 코드 :
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());
}
}
}
}