本文介绍了用户关闭(Xs out)JFrame后立即执行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
基本上它是带有GUI的客户端程序,所以我想在用户关闭客户端程序时关闭套接字。是否有监听器或某些东西可以让我这样做?
Basically it's a client program with a GUI so I want to close the sockets when the user closes the client program. Is there is Listener or something that will allow me to do this?
推荐答案
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// do stuff
}
});
请注意,默认关闭操作时,只会调用 在框架通过(x)按钮关闭之前设置为 EXIT_ON_CLOSE
。默认值为 HIDE_ON_CLOSE
,从技术上讲,它不会关闭窗口,因此不会通知监听器。
Note that this will only be called when the default close operation has been set to EXIT_ON_CLOSE
before the frame is closed via the (x) button. The default is HIDE_ON_CLOSE
which technically does not close the window, therefore the listener would not be notified.
这篇关于用户关闭(Xs out)JFrame后立即执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!