在“模型-视图-控制器"中,为什么更改模型而不触发视图的更改? [英] In Model-View-Controller, Why change in model, doesn't trigger the change in view?

查看:82
本文介绍了在“模型-视图-控制器"中,为什么更改模型而不触发视图的更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发自己的扫雷器.摇摆遵循Model-View-Controller设计模式.在 MVC 中,我了解了每当模型发生更改时,控制器也会触发视图更改.但是在此示例中,我无法跟踪如何在setTitlesetInfo中进行更改以反映在视图中.

I am currently developing my own minesweeper. Swing follows Model-View-Controller design pattern. In MVC, I learnt whenever there is a change in model, the controller will trigger that change in view too. But In this example, I cannot trace how to make the changes in setTitle and setInfo to get reflected in view.

在这里,当我设置对话框的标题时,实际的内容(模型)正在更改,但是输出(视图)中没有相应的更改.

Here, when I set the title of the Dialog box, the actual content(model) is getting changed, But there is no corresponding change in the output(view).

//InfoDisplayer is inner class of class MenuActionListener
class InfoDisplayer extends JDialog { 
    JLabel info;
    BorderLayout infoBorderLayout = new BorderLayout();

    public InfoDisplayer(JFrame ownerFrame) {
        super(ownerFrame,true); 
        info = new JLabel();
        setFocusable(false);                        
        setSize(300,400);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLayout(infoBorderLayout);
        add(info,BorderLayout.SOUTH);
        setVisible(true);
    }

    void setInfo(JLabel info) {
        this.info = info;
    }

    public void setTitle(String title) {
        super.setTitle(title);
    }                                   
}

if ((event.getActionCommand()).equals("HowToPlay")) {
    InfoDisplayer instructionsDisplay = new InfoDisplayer(gUIManagerFrame); 
    //gUIManagerFrame is an object of its outer class,MenuActionListener
    instructionsDisplay.setTitle("INSTRUCTIONS");
    instructionsDisplay.setInfo(new JLabel("<html><h1><B>INSTRUCTIONS</B></h1></html>"));
} else {// if about is clicked!!
    InfoDisplayer aboutDisplay = new InfoDisplayer(gUIManagerFrame);
    aboutDisplay.setTitle("MineSweeper v0.1");
    aboutDisplay.setInfo(new JLabel("<html><h1><B>MineSweeperv1.0</B></h1> </html>"));
}           

推荐答案

只要模型发生变化,控制器就会触发 view 中的变化.

Whenever there is a change in model, the controller will trigger that change in view.

模型-视图-控制器模式中,当控制器更新模型模型通常会使用此处.

In the Model–View–Controller pattern, when the controller updates the model, the model will notify the view, typically using the observer pattern, and the view then updates itself. The view may interrogate the model and process any resulting update. There's a more detailed answer and example here.

这篇关于在“模型-视图-控制器"中,为什么更改模型而不触发视图的更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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