Singleton Gui 框架会导致堆栈溢出吗?如果不是,所有加载的数据在 GUI 帧之间是否都会丢失?

发布于 2025-01-12 19:50:55 字数 2026 浏览 3 评论 0原文

我一直在开发一个类似于 dnd 字符表的应用程序,但我讨厌使用 GUI。我想过使用与 MVC 模式类似的东西来处理 GUI 框架(如何加载或隐藏面板),但在我的方法中,我最终将其与 Singleton 模式结合起来,建立一个名为 GUI_Controller 的基类,然后加载所有面板通过方法调用。这让我可以将每个新的“页面”或“工作表”创建为一个单独的类,当我需要“加载”它时,我所要做的就是使用 Singleton GUI_Controller 中的方法调用。这种方法似乎很有效,而且我喜欢它如何使 GUI 框架易于遵循。我的问题是,这样可以吗?或者这只是暂时有效?我不知道控件的工作方式是否有效,如果“视图”随着越来越多的类方法调用而不断移动,这是否会导致堆栈溢出错误。或者,如果每次返回到单例,堆栈都保持干净,但导致一个类的调用“加载”的任何数据被“擦除”,然后在再次调用该类的调用时“重新加载”(例如:单击咒语页面,返回角色页面,然后再次单击咒语页面)。

如果是这样,则通过连接 SQL 数据库的“Database”类调用数据。那么,在最初将范围移动到单例之前,最好先加载数据库吗?

GUI_Controller 的 Singleton 看起来像这样......

public class GUI_Controller extends JFrame{
   private static GUI_Controller singleton = new GUI_Controller();

private GUI_Controller() {
    initialize();
}

public static GUI_Controller getSingleton() {
    if(singleton == null) {
        singleton = new GUI_Controller();
    }
    return singleton;
}

private JFrame frame;


private void initialize() {
    frame = new JFrame();
    frame.setLocation(100,100);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
    
}


public void setHomePage() {
    HomePage hp = new HomePage();
    
    frame.getContentPane().removeAll();
    frame.getContentPane().add(hp);
    
    frame.pack();
    frame.setVisible(true);
}


public void setSpellsPage() {
    SpellsPage sp = new SpellsPage();
    
    frame.getContentPane().removeAll();
    frame.getContentPane().add(sp);
    
    frame.pack();
    frame.setVisible(true);
}

其他类以类似的方式访问 Singleton

GUI_Controller gui = GUI_Controller.getSingleton();
    gui.setHomePage();

btnViewSpells.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            GUI_Controller gui = GUI_Controller.getSingleton();
            gui.setSpellsPage();
        }
    });

我将添加一个注释,即每个类不是单个 JPanel,而是充当具有自己的设计和卡片布局视图的“框架”。我想出了这个整体框架来简化GUI控制,但现在我不知道这是否只是过度编程。

I've been working on an app that's similar to a dnd character sheet, and I hate working with GUI's. I thought of using something not unlike a MVC pattern for handling the GUI framework (how panels are loaded or hidden), but in my approach I ended up combining it with a Singleton pattern to establish a base class called GUI_Controller that all panels were then loaded onto via method calls. This let me create each new "page" or "sheet" as a separate class, and when I needed it to be "loaded" all I had to do was use the method call within the Singleton GUI_Controller. This approach seems to work, and I like how it makes the framework of the GUI easy to follow. My question is, is this OK? or is this only working for now? I didn't know if the way the control works, if this could lead to a stack overflow error if the "view" kept shifting enough with more and more method calls to classes stacking up. Or if by returning to the Singleton each time, the stack was being kept clean, but resulting in any data "loaded" by one class's call being "erased" and then "re-loaded" when that class's call was called again (ex: clicking on the spells page, going back to character page, then clicking on spells page again).

If so, the data is called via a "Database" class that connects an SQL database. Would it then be best to front load the database before initially moving scope to the Singleton?

The Singleton for GUI_Controller looks like this..

public class GUI_Controller extends JFrame{
   private static GUI_Controller singleton = new GUI_Controller();

private GUI_Controller() {
    initialize();
}

public static GUI_Controller getSingleton() {
    if(singleton == null) {
        singleton = new GUI_Controller();
    }
    return singleton;
}

private JFrame frame;


private void initialize() {
    frame = new JFrame();
    frame.setLocation(100,100);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
    
}


public void setHomePage() {
    HomePage hp = new HomePage();
    
    frame.getContentPane().removeAll();
    frame.getContentPane().add(hp);
    
    frame.pack();
    frame.setVisible(true);
}


public void setSpellsPage() {
    SpellsPage sp = new SpellsPage();
    
    frame.getContentPane().removeAll();
    frame.getContentPane().add(sp);
    
    frame.pack();
    frame.setVisible(true);
}

and other classes access the Singleton in ways like

GUI_Controller gui = GUI_Controller.getSingleton();
    gui.setHomePage();

and

btnViewSpells.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            GUI_Controller gui = GUI_Controller.getSingleton();
            gui.setSpellsPage();
        }
    });

I'll add a note that each class is not a single JPanel, but acts as a "frame" having it's own design and card layout views. I came up with this overall framework to simplify the GUI control, but now I don't know if it's just over-programming.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文