Java使用MVC单击计数器

use*_*409 5 java model-view-controller

我正在尝试编写一个非常简单的Java示例来学习MVC.这是一个JButton,当点击时会增加一个计数器并显示到目前为止的点击次数.

我把模型,视图和控制器分成了不同的类,并且认为我在正确的路径上,但是当我点击按钮时,显示计数器的JLabel继续保持为0.

有人可以快速查看为什么显示点击次数的JLabel始终保持为0?

谢谢

View

package mvc;  
import javax.swing.JButton;  
import javax.swing.JLabel;  
import javax.swing.JTextArea;  
import javax.swing.SwingUtilities;  
import javax.swing.WindowConstants;  


public class View extends javax.swing.JFrame {  
    private JButton jButton1;  
    private JLabel jLabel1;  
    private Controller c;   
    private Model m;  

    /**
    * Auto-generated main method to display this JFrame
    */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Controller c = new Controller();    

                Model m = new Model();

                View inst = new View(c,m);
                inst.setLocationRelativeTo(null);
                inst.setVisible(true);
            }
        });
    }

    public View(Controller c, Model m) {
        super();
        this.c = c;     
        this.m = m;
        initGUI();
    }

    private void initGUI() {
        try {
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            getContentPane().setLayout(null);
            {
                jButton1 = new JButton();
                getContentPane().add(jButton1, "Center");
                jButton1.setText("Click");
                jButton1.setBounds(314, 180, 101, 34);
                jButton1.addActionListener(c);
            }
            {
                jLabel1 = new JLabel();
                getContentPane().add(getJLabel1());
                jLabel1.setText("Click Count = " + c.getClickCount());
                jLabel1.setBounds(439, 183, 91, 27);

            }
            pack();
            this.setSize(818, 414);
        } catch (Exception e) {
            //add your error handling code here
            e.printStackTrace();
        }
    }

    public JLabel getJLabel1() {
        return jLabel1;
    }   
}

End View

Controller class

package mvc;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Controller implements ActionListener 
{   
    Model m;
    View v;

    public Controller()
    {
        m = new Model();        
        v = new View(this, m);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) 
    {
        if (arg0.getSource() == "Click")
        {
            m.addClick();
            v.getJLabel1().setText("Click count = " + getClickCount());
        }

    }

    public int getClickCount()
    {
        return m.getClicks();
    }
}

End Controller class

Model class

package mvc;

public class Model 
{
    private int clicks;

    public Model()
    {
        clicks = 0;
    }

    public void addClick()
    {
        clicks++;
    }

    public int getClicks()
    {
        return clicks;
    }
}

End Model class
Run Code Online (Sandbox Code Playgroud)

Sur*_*ran 0

有几个问题:

  • 视图显示的实际数据通常来自模型而不是来自控制器。

    所以你的视图中的代码
    jLabel1.setText("Click Count = " + c.getClickCount());
    应该更改为
    jLabel1.setText("Click Count = " + m.getClickCount());

  • 在控制器内,您创建模式和视图的新实例,并在 main() 方法内再次创建控制器和视图的新实例。因此本质上控制器类正在处理不同的视图和模型对象。

一般来说,但这不是一个刻板的标准:

  • 视图有一个模型,它不引用控制器
  • 模型是独立的,不引用视图或控制器
  • 控制器同时具有视图和模型。