OnExit Event For a Swing Application?

我的未来我决定 提交于 2019-11-27 16:08:36

问题


I'm developing a simple application to manage the operational part of a business using Swing, but I need that when the application exits, it performs this:

updateZonas();
db.close();

But how can I do this?


回答1:


Runtime.getRuntime().addShutdownHook(new Thread()
{
    @Override
    public void run()
    {
        updateZonas();
        db.close();
    }
});

This works for any Java application(Swing/AWT/Console)




回答2:


Are you using a JFrame? if so you can try this:

    myframe.addWindowListener(new java.awt.event.WindowAdapter() {
        public void windowClosing(WindowEvent winEvt) {
            updateZonas();
            db.close();
            System.exit(0);
        }
    });



回答3:


Add a WindowListener to your JFrame. Its windowClosing method would call whatever code you need, then System.exit(0) (or some other return code).



来源:https://stackoverflow.com/questions/2467070/onexit-event-for-a-swing-application

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!