栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

用于关闭JFrame并使用全局变量的WindowListener

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

用于关闭JFrame并使用全局变量的WindowListener

在过去,当我遇到相同的问题时,我决定实现一个Singleton模式,以使用户当前的会话保持“全局”。这样,我就可以访问我需要的任何课程中的当前会话。

应该是这样的:

public class SessionManager {    private static SessionManager instance;    private Session currentSession; // this object holds the session data (user, host, start time, etc)    private SessionManager(){ ... }    public static SessionManager getInstance(){        if(instance == null){ instance = new SessionManager();        }        return instance;    }    public void startNewSession(User user){        // starts a new session for the given User    }    public void endCurrentSession(){        // here notify the server that the session is being closed    }    public Session getCurrentSession(){        return currentSession;    }}

然后我调用

endCurrentSession()
内部
windowClosing()
方法,如下所示:

public void windowClosing(WindowEvent e) {    SessionManager.getInstance().endCurrentSession();}

注意:
在此调用此方法将在事件分配线程中执行,导致GUI“冻结”,直到完成此方法为止。如果您与服务器的交互花费很长时间,则需要在单独的线程中进行。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/419653.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号