如何使用Swing GUI启动Java MVC应用程序 [英] How to Start a Java MVC Application With a Swing GUI

查看:95
本文介绍了如何使用Swing GUI启动Java MVC应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们假设我们有一个简单的Java MVC应用程序,其类为ModelViewController. View类直接从 JFrame .与经典MVC设置一样,该视图也具有参考模型和控制器对视图和模型的引用. 正如我刚刚了解到的,所有与GUI相关的内容都应包装在

Let us assume we have a simple Java MVC Application with the classes Model, View and Controller. The View class directly inherits from JFrame. As in a classic MVC setup, the view has a reference to the model and the controller has a reference to the view and to the model. As I just learnt, all GUI-related stuff should be wrapped in a SwingUtilities.invokeLater or something similar. Now what is the right way to initialise/start this application? I think the creation of the model and the controller should not be inside of the EDT, right? So I would come up with something like this:

final Model model = new Model();
SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
        final View view = new View(model);
        new Thread(new Runnable() {
            @Override
            public void run() {
                new Controller(model, view);
            }
        }).start();
    }
});

这是正确的方法和好主意吗?还是有更好的可能性?

Is this the correct way and a good idea or are there better possibilities?

正如@trashgod正确指出的那样,在此处检查了一个相关示例.然后,我扩展我的问题:基本上完成了以下操作:

As correctly stated by @trashgod, a related example is examined here. Then I extend my question: What is basically done there is the following:

SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
        Model model = new Model();
        View view = new View(model);
        new Controller(model, view);
    }
});

但是在EDT中运行整个应用程序不是错吗?

But isn't it wrong to run the whole application in the EDT?

推荐答案

与Swing组件创建或交互的所有代码都必须在事件分发线程上运行.因此,您的代码的第二种形式,即下面的代码是正确的.

All the code that creates or interacts with Swing components must run on the event dispatch thread. So the second form of your code, that is the bellow code is correct.

`SwingUtilities.invokeLater(new Runnable() {
        @Override
    public void run() {
        Model model = new Model();
        View view = new View(model);
        new Controller(model, view);
    }
});`

必须通过EDT或辅助线程运行所有UI代码的原因是为了避免出现多线程问题.您可能会看到许多Swing程序可能未在EDT中初始化代码.完全可以但是,当您的挥杆动作完全完成时,就有可能出错.我在从主线程开始倾斜的简单秋千应用程序中的自我,我没有遇到死锁的情况.快速任务使用EDT,而长时间运行的任务使用辅助线程. 此处是ui上多线程问题的链接

The reason for all UI code that must run through EDT or worker thread is to avoid Multithreaded problem. You may see many swing programs may not init code in EDT. This is perfectly ok. But when your swing gets complecated, then there is probability for error. my self in simple swing apps lanching from main thread, i did not face dead locks are race conditions. Quick tasks uses EDT and long running tasks uses worker threads. Here is link on multithreaded problem on ui

如果我做错了,请告诉我

Please let me know if i went wrong

这篇关于如何使用Swing GUI启动Java MVC应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆