因此,我试图创建一个简单的测试程序,用户可以在其中输入一些内容到JTextField中,单击“添加” JButton,然后JTextArea将用户字符串添加到JTextArea中(连续添加新行)。

我为按钮添加了actionListener,并具有stateChanged和update方法,但是当我单击添加按钮时没有任何反应。也没有错误。有人能指出我正确的方向吗?

这是我的代码:

MVCTester(主营)

public class MVCTester {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        MVCController myMVC = new MVCController();
        MVCViews myViews = new MVCViews();
        myMVC.attach(myViews);
    }

}


MVC控制器

import java.util.ArrayList;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


public class MVCController {

    MVCModel model;
    ArrayList<ChangeListener> listeners;
    public MVCController(){
        model = new MVCModel();
        listeners = new ArrayList<ChangeListener>();
    }

    public void update(String input){
        model.setInputs(input);
          for (ChangeListener l : listeners)
          {
             l.stateChanged(new ChangeEvent(this));
          }
    }
     public void attach(ChangeListener c)
     {
        listeners.add(c);
     }
}


MVC模型

import java.util.ArrayList;

public class MVCModel {
    private ArrayList<String> inputs;


    MVCModel(){
        inputs = new ArrayList<String>();

    }

    public ArrayList<String> getInputs(){
        return inputs;
    }

    public void setInputs(String input){
        inputs.add(input);
    }

}


MVC视图

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


public class MVCViews implements ChangeListener {


    private JTextField input;
    private JTextArea echo;
    private ArrayList<String> toPrint = new ArrayList<String>();
    MVCController controller;

    MVCViews(){
        controller = new MVCController();
        JPanel myPanel = new JPanel();
        JButton addButton = new JButton("add");
        echo = new JTextArea(10,20);
        echo.append("Hello there! \n");
        echo.append("Type something below!\n");
        myPanel.setLayout(new BorderLayout());
        myPanel.add(addButton, BorderLayout.NORTH);
        input = new JTextField();
        final JFrame frame = new JFrame();
        frame.add(myPanel, BorderLayout.NORTH);
        frame.add(echo, BorderLayout.CENTER);
        frame.add(input, BorderLayout.SOUTH);

        addButton.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                controller.update(input.getText());
            }

        });

        frame.pack();
        frame.setVisible(true);

    }

    @Override
    public void stateChanged(ChangeEvent e) {
        // TODO Auto-generated method stub
        toPrint = controller.model.getInputs();

        for(String s: toPrint){
            echo.append(s + "\n");
        }

    }
}


这是我第一次尝试遵循MVC格式,因此模型本身也可能存在问题。随时指出他们。谢谢您的帮助!

最佳答案

GUI中的控制器与main中创建的控制器不同。请注意,您在上面的代码中调用new MVCController()的次数是两次。每次执行此操作时,您都在创建一个新的独特控制器-不好。只使用一个。您必须将一个控制器传递给视图。您可以弄清楚该如何做。 (提示,setter或构造函数参数将起作用)。

提示2:这可能有效:MVCViews myViews = new MVCViews(myMVC);

一种解决方案:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class MVCTester {
   public static void main(String[] args) {
      MVCController myMVC = new MVCController();
      MVCViews myViews = new MVCViews(myMVC);
      myMVC.attach(myViews);
      // myViews.setController(myMVC); // or this could do it
   }
}

class MVCController {
   MVCModel model;
   ArrayList<ChangeListener> listeners;

   public MVCController() {
      model = new MVCModel();
      listeners = new ArrayList<ChangeListener>();
   }

   public void update(String input) {
      model.setInputs(input);
      for (ChangeListener l : listeners) {
         l.stateChanged(new ChangeEvent(this));
      }
   }

   public void attach(ChangeListener c) {
      listeners.add(c);
   }
}

class MVCModel {
   private ArrayList<String> inputs;

   MVCModel() {
      inputs = new ArrayList<String>();
   }

   public ArrayList<String> getInputs() {
      return inputs;
   }

   public void setInputs(String input) {
      inputs.add(input);
   }
}

class MVCViews implements ChangeListener {
   private JTextField input;
   private JTextArea echo;
   private ArrayList<String> toPrint = new ArrayList<String>();
   MVCController controller;

   MVCViews(final MVCController controller) {
      // !! controller = new MVCController();
      this.controller = controller;
      JPanel myPanel = new JPanel();
      JButton addButton = new JButton("add");
      echo = new JTextArea(10, 20);
      echo.append("Hello there! \n");
      echo.append("Type something below!\n");
      myPanel.setLayout(new BorderLayout());
      myPanel.add(addButton, BorderLayout.NORTH);
      input = new JTextField();
      final JFrame frame = new JFrame();
      frame.add(myPanel, BorderLayout.NORTH);
      frame.add(echo, BorderLayout.CENTER);
      frame.add(input, BorderLayout.SOUTH);
      addButton.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent e) {
            if (controller != null) {
               controller.update(input.getText());
            }
         }
      });
      frame.pack();
      frame.setVisible(true);
   }

   public void setController(MVCController controller) {
      this.controller = controller;
   }

   @Override
   public void stateChanged(ChangeEvent e) {
      if (controller != null) {
         toPrint = controller.model.getInputs();
         for (String s : toPrint) {
            echo.append(s + "\n");
         }
      }
   }
}

07-24 22:24