OnExit Event For a Swing Application?

前端 未结 3 1983
盖世英雄少女心
盖世英雄少女心 2020-12-15 22:25

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:

         


        
相关标签:
3条回答
  • 2020-12-15 22:35

    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);
            }
        });
    
    0 讨论(0)
  • 2020-12-15 22:35

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

    0 讨论(0)
  • 2020-12-15 22:43
    Runtime.getRuntime().addShutdownHook(new Thread()
    {
        @Override
        public void run()
        {
            updateZonas();
            db.close();
        }
    });
    

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

    0 讨论(0)
提交回复
热议问题