且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 JFrame 关闭时终止正在运行的线程

更新时间:2022-10-25 14:25:58

Using JFrame.EXIT_ON_CLOSE actually terminates the JVM (System.exit). All running threads will automatically be stopped.

If you want to perform some action when a JFrame is about to close, use a WindowListener.

JFrame frame = ...
frame.addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosing(WindowEvent e) {
        // close sockets, etc
    }
});