MVC模型中的多线程GUI [英] Multithreaded GUI in the MVC model

查看:92
本文介绍了MVC模型中的多线程GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于MVC模型中的GUI以及将代码放置在何处以及在哪个线程中的问题,我已经进行了大量的发布.

I have gone through a ton of postings regarding GUI in the MVC model and where to put code and in which thread.

MadProgrammer和rashgodd使我朝着正确的方向前进,但是有些事情我还是不明白.

MadProgrammer and trashgod have put me in the right direction, but there are things I still don't understand.

在我的主班里,我有以下代码

In my main class I have the following code

import java.awt.EventQueue;

import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import common.InitParameters;
import common.OraLogin;
import common.ThresholdValues;


public class cmtNew implements Runnable {
    public static void main(String[] args) {
        EventQueue.invokeLater(new cmtNew());
    }

    @Override
    public void run() {

        // Set look and feel of GUI
        try {
            UIManager.setLookAndFeel(
                    UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException
                | IllegalAccessException | UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }

        // Login to database
        loginDb();  

        // Init system parameters
        initParameters();

        // Set up the application
        CmtModel theModel       = new CmtModel();
        CmtGui theGui           = new CmtGui();
        CmtControl theControl   = new CmtControl(theGui,theModel);

//        Model model           = new Model();
//        View view             = new View(model);
//        Control control       = new Control(model, view);

        // Show GUI
        theGui.setVisible(true);

    }

    /**
     * Login to database
     */
    private static void loginDb(){
        OraLogin login = new OraLogin();
        try {
            login.userLogin();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Unable to log in to database: "+e.getMessage());
            System.exit(0);
        }
    }

    /**
     * Init parameters
     */
    private static void initParameters(){
        try {
            InitParameters initParameters = new InitParameters();
            ThresholdValues thresholdValues = new ThresholdValues();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Unable initialize environment parameters: "+e.getMessage());
            System.exit(0);
        }
    }
}

我在控件的构造函数中启动了多个SwingWorker线程,以处理对数据库的慢查询.

I start up a number of SwingWorker threads in the constructor of the Control to take care of slow queries to the database.

问题:

  1. 初始化模型,视图,控制时被注释掉的部分来自MVCgame的示例,由rashgod https://stackoverflow.com/a/3072979/4654417 为什么将模型传递给视图?

  1. The part that is commented out when initializing model, view, control is from the MVCgame example by trashgod https://stackoverflow.com/a/3072979/4654417 why pass the model to the view?

GUI更新应该在EDT中完成,但是将控件,模型,视图init放在代码中的位置(显然是惯例),所有这些都将在EDT上... ,还是?

The GUI updates is supposed to be done in the EDT, but putting the control, model, view init where it is in the code (apparently common practice), all of them will be on the EDT..., or?

我已将SwingWorker线程放入控件中.对还是错?好还是坏?

I have put the SwingWorker threads in the Control. Right or wrong? Good or bad?

作为非OO程序员的背景,我仍然很难理解事件处理的概念. 我已经以控件告诉模型查询数据库中数据的方式实现了MVC.然后,Control向Model索要数据,并以更新方法将其传递给View.我的理解是……这是错误的解决方法.

My background as a non OO programmer I still have a hard time to wrap my head arround the concept of event handling. I have implemented MVC in the way that Control tell Model to query for data in the database. Then Control ask Model for data and passes that on to View in an update method. What I understand..., that's the wrong way to go about it.

我认为应该工作的方式是Control某种程度上告诉Model查询数据/读取文件或其他内容.如果Model成功完成,则会触发一个事件,提示嘿,我有新数据". View(或Control)侦听事件并以某种方式更新EDT线程中的gui.

How I believe it kind of should work is that Control somehow tell Model to query data/read a file or whatever. If Model successfully can do that it triggers an event that says 'hey I have fresh data'. View (or Control) listens for the event and update the gui in the EDT thread somehow.

我只是无法弄清楚事件的流量以及谁(模型,控件,视图)在做什么和如何做.是的,我研究了Oracle的大量教程,但仍然不了解.

I just cant figure out the event traffic and who (Model, Control, View) is doing what and how. And yeeees, I have studied the numerous tutorials from Oracle but I still don't get it.

欢呼

推荐答案

我希望我不会与MadProgrammer和rashgodd之前所说的相抵触.

I hope I'm not contradicting anything MadProgrammer and trashgod have previously said.

为什么将模型传递给视图?

Why pass the model to the view?

模型必须对视图不了解.这样一来,该模型就可以用于多种视图类型(Swing,Web,智能手机).

The model must remain ignorant of the view. This allows the model to be used with more than one type of view (Swing, web, smart phone).

但是,视图可以读取.视图大多数不会更新模型.更新模型是控制器的工作.

However, the view can read from the model. The view most not update the model. Updating the model is the job of the controller.

GUI更新应该在EDT中完成,但是将控件,模型,视图,init放在代码中的位置(显然是惯例),所有这些都将在EDT上吗?

The GUI updates [are] supposed to be done in the EDT, but putting the control, model, view, init where it is in the code (apparently common practice), all of them will be on the EDT?

必须在EDT上完成GUI的构建和更新.通常,您将具有操作侦听器和/或项目侦听器,用于侦听GUI更改.侦听器中的代码将更新模型,并在必要时重新绘制/重新验证GUI.

Constructing and updating the GUI must be done on the EDT. Generally, you'll have action listeners and / or item listeners that listen for GUI changes. The code in the listeners will update the model and repaint / revalidate the GUI if necessary.

基本上,您的听众就是您的控制者.我建议每个GUI组件一个或多个侦听器,而不是尝试处理多个GUI组件的超级侦听器.

Basically, your listeners are your controllers. I'd suggest one or more listeners per GUI component, rather than a super listener that tries to handle multiple GUI components.

我已将SwingWorker线程放入控件中.

I have put the SwingWorker threads in the Control.

通常,您的SwingWorker线程是控制器的一部分.通常,我将线程与其他所有线程放在单独的类中.

Generally, your SwingWorker threads are part of the the controller. Generally, I put threads in separate classes from everything else.

我已经以Control告诉Model查询数据库中数据的方式实现了MVC.然后Control向Model询问数据,并以更新方法将其传递给View.

I have implemented MVC in the way that Control tell Model to query for data in the database. Then Control ask Model for data and passes that on to View in an update method.

这是我在Swing GUI中实现MVC的方式.

Here's how I implement MVC in a Swing GUI.

  • 该视图可以从模型中读取.
  • 该视图可能不会更新模型.
  • 控制器将更新模型.
  • 控制器将重新绘制视图/重新验证视图.

要想更好地了解如何在Swing应用程序中实现MVC模型,请查看我的

For a better idea of how I implement the MVC model in a Swing application, take a look at my Retro Snake Game article.

这篇关于MVC模型中的多线程GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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