GUI 中的观察者/可观察者 [英] Observer/Observable in a GUI

查看:41
本文介绍了GUI 中的观察者/可观察者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不久前我问了一个类似的问题,我正在尝试实施该解决方案.我有一个 JPanel,它最终会有许多控件来生成一个数字,但现在它只有一个 JButton 作为测试.我正在使用 Observer 和 Observable 让 JPanel 让 JFrame 知道发生了一些变化.我似乎能够告诉观察者观察某些东西,但 update() 似乎没有调用观察者对象.我不确定自己哪里出错了,几乎不可能找到关于 Observer/Observable 的好的教程.

I asked a similar question a while back and I'm trying to implement the solution. I have a JPanel that will eventually have a number of controls on it to generate a number, but right now it just has a single JButton as a test. I am using Observer and Observable to have that JPanel let the JFrame know something has changed. I seem to be able to tell the Observer to watch for something, but the update() doesn't seem to call the Observer object. I am not sure where I am going wrong and finding a good tutorial on Observer/Observable is almost impossible.

Object Subpanel 扩展了 Observable,Subpanel 里面是我添加到 JFrame 的 JPanel.这似乎是我可以让 GUI 也扩展 Observable 的唯一方法.

The Object Subpanel extends Observable and inside Subpanel is the JPanel that I add to the JFrame. This seems to be the only way I can get a GUI to also extend Observable.

如果这听起来令人困惑,我很抱歉.我不知道如何解释它.我只希望 GUI 菜单对象能够在发生更改时通知 JFrame.

I'm sorry if this sounds confusing. I'm not sure how else to explain it. I just want an GUI menu object to be able to notify the JFrame when a change occurs.

import java.awt.FlowLayout;
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.JPanel;


public class TestObserver implements Observer{

    JFrame frame = new JFrame();

    //The panel in the frame that is to be watched for a change.
    SubPanel sf = new SubPanel();


    TestObserver(){

        frame.setTitle("New Program!");
        frame.setSize(300, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());


        sf.addOutsideObserver(this);

        frame.add(sf.panel);
        frame.setVisible(true);
    }

    @Override
    public void update(Observable arg0, Object arg1) {
        // TODO Auto-generated method stub
        System.out.println("I have been notified!");
    }

    public static void main(String[] args) {
        TestObserver mf = new TestObserver();
    }
}

class SubPanel extends Observable implements ActionListener{
    JPanel panel = new JPanel();
    JButton b = new JButton();
    int count = 0;

//  Observer ob = new Observer();

    SubPanel(){
        b.addActionListener(this);
        panel.add(b);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        // The counter is not important, just something to display
        count++;
        System.out.println("Count: " + count);
        // This does not seem to be happening in the Observer object
        notifyObservers();
    }

    public void addOutsideObserver(Observer o){
        addObserver(o);
        System.out.println("I'm added!");
    }
}

推荐答案

notifyObservers() acts 仅在发生更改后(如 hasChanged() 方法).

notifyObservers() acts only after a change occurs (as verified by hasChanged() method).

在您的代码中,您需要添加 setChanged() 以便在调用 notify 之前设置更改指示器.

In your code you need to add setChanged() in order to set the change indicator before calling notify.

不需要调用clearChanged() 之后,因为此方法由 notifyObservers 方法自动调用.

You do not need to call clearChanged() afterwards since this method is called automatically by the notifyObservers methods.

这篇关于GUI 中的观察者/可观察者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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