0

问候,

我一直在研究模型-视图-控制器的 Obsser/Observable 实现。我也可能误用了这种模式,但这是我到目前为止所做的。在我的代码中。当 Submit 被按下时,它会触发 Model 的 makeChange()。但永远不会触发测试的 update()。

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Testing implements Observer {

    public Testing() {
        model.addObserver(this);
        loadListener1();        
    }

    private void loadListener1() {
        view1.submitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                model = new Model(view1.data1Field.getText(), view1.data2Field.getText());
                model.makeChange();
                model.notifyObservers();
            }
        });
    }  

    public void update(Observable o, Object arg) { System.out.println("Notify - Observer"); }

    public static void main(String[] a) { Testing testing = new Testing(); }

    private View view1 = new View("1");    
    private Model model = new Model();

}

class View extends JFrame  {

    public View(String frame) {
        super("Frame: " + frame);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(300, 300);
        setVisible(true);
        setLayout(new GridLayout(3, 2));
        add(data1Label); add(data1Field);
        add(data2Label); add(data2Field);
        add(submitButton); add(cancelButton);
    }

    private final JLabel data1Label = new JLabel("Data1");
    private final JLabel data2Label = new JLabel("Data2");
        public final JTextField data1Field = new JTextField();
    public final JTextField data2Field = new JTextField();
    public final JButton submitButton = new JButton("Submit");
    public final JButton cancelButton = new JButton("Cancel");

}

class Model extends Observable {

    public Model() { }
    public Model(String data1, String data2) {
        setData1(data1);
        setData2(data2);
    }
    public String getData1() { return data1; }
    public final void setData1(String data1) { this.data1 = data1; }
    public String getData2() { return data2; }
    public final void setData2(String data2) { this.data2 = data2; }
    public void makeChange() {
        setChanged();
        notifyObservers();
        System.out.println("Notify - Observable");
    }
    private String data1;
    private String data2;

}

你能指导我通过吗?非常感谢您的回复。

谢谢,西里尔 H。

4

1 回答 1

0

在类的构造函数中,将您的寄存器本身测试为在字段初始化期间创建的模型实例。

在提交按钮的 actionPerformed 方法中,您创建了一个的模型实例,然后在这个新实例上调用没有注册观察者的方法 notifyObservers!

试试这个代码:

private void loadListener1() {
    view1.submitButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            model.setData1(view1.data1Field.getText()),
            model.setData2(view1.data2Field.getText());
            model.makeChange();
        }
    });
}
于 2011-01-14T16:35:22.050 回答