观察者/可观察的帮助!

发布于 2024-10-12 08:49:04 字数 2576 浏览 6 评论 0原文

您好,

我一直在研究模型-视图-控制器的观察者/可观察实现。 我也可能滥用了该模式,但这就是我到目前为止所做的。 在我的代码中。当按下提交时,它会触发模型的 makeChange()。 但永远不会触发Testing的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.

Greetings,

I have been studying Obserser/Observable implementation to Model-View-Controller.
I may also misused the pattern but here's what I've done so far.
In my code. When Submit was press, it triggers the makeChange() of the Model.
But never triggers the update() of Testing.

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;

}

Can you guide me thru?
Your reply is highly appreciated.

Thanks,
Cyril H.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨的味道风的声音 2024-10-19 08:49:04

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

在提交按钮的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();
        }
    });
}

In constructor of clas Testing your register itself to instance of Model that is created during field initialisation.

In actionPerformed method of submit button you creatd a new instance of Model and later call on this new instance the method notifyObservers which has no registered observers!

Try this code:

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();
        }
    });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文