使用ChangeListener触发Java Swing中的更改? [英] using ChangeListener to fire changes in Java Swing?

查看:617
本文介绍了使用ChangeListener触发Java Swing中的更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实施猜词游戏。所附图像给出了我在做什么的想法。我的 GamePane 由两个组件组成, ControlPane HangManPane ,这是附件图像的顶部和底部。当玩家单击新游戏按钮时,必须通知 GamePane 。随后, GamePane 将从 ControlPane 请求SecretWord,并将其传递给 HangManPane 来构建模型。

I am implementing a word guessing game. The attached image gives an idea of what I am doing. My GamePane consists of two components, ControlPane and HangManPane, which is the top and bottom section of the attached image. when the player clicks, New Game button, the GamePane must be notified. subsequently, the GamePane will request the SecretWord from ControlPane and pass it on to HangManPane to construct the model.

所以这里发生了两件事,我想知道如何实现

so two things happen here which I would like to know how to implement


  1. ControlPane应该在用户单击新游戏按钮时触发通知。因此,此fireChange应该发生在新游戏按钮。

GamePane 侦听通知并将信息传递给 HangManPane

GamePane listens to the notifications and pass the information to HangManPane

使用 ChangeListener 是合适的。我做了搜索的一部分,但无法掌握如何在此处实施。欢迎任何建议

Using ChangeListener would be appropriate. I did my part of searching, but unable to grasp on how to implement here. Any suggestions are kindly welcome

public class GamePane extends JPanel {

    public GamePane(){
         ControlPane cp = new ControlPane();
         //if user clicks New Game on ControlPane, notify me
         //I will then do the following 
         HangManModel model = new DefaultHangManModel(cp.getSecretWord());
         HangManPane hangManPane = new HangManPane(model);

         setLayout(new GridLayout(0,1));
         this.add(cp);
         this.add(pane);

    }
 }

推荐答案

提供监听器支持相对简单。 JComponent 公开了 EventListenerList listenerList )作为受保护的变量。

Providing listener support is "relatively" simple. It's simplified by the fact the JComponent exposes it's EventListenerList (listenerList) as a protected variable.

ControlPane ,则需要一个add方法...

In the ControlPane, you'll need an add method...

public void addChangeListener(ChangeListener listener) {
    listenerList.add(ChangeListener.class, listener);
}

您将需要删除方法

public void removeChangeListener(ChangeListener listener) {
    listenerList.remove(ChangeListener.class, listener);
}

现在,您需要某种方式来根据需要实际引发或触发事件。

Now, you need some way to actually raise or fire events as needed...

protected void fireStateChanged() {
    ChangeListener[] listeners = listenerList.getListeners(ChangeListener.class);
    if (listeners != null && listeners.length > 0) {
        ChangeEvent evt = new ChangeEvent(evt);
        for (ChangeListener listener : listeners) {
            listener.stateChanged(evt);
        }
    }
}

现在,无论何时需要告诉注册的侦听器 ControlPane 状态已更改,您只需调用 fireStateChanged ,例如...

Now, when ever you want to tell registered listeners that the ControlPane state has changed, you would simply call fireStateChanged, for example...

public void actionPerformed(ActionEvent evt) {
    fireStateChanged();
}

现在,在 GamePane ,您将需要针对 ControlPane ...

Now, in the GamePane, you will need to register a ChangeListener against the instance of the ControlPane...

private ControlPane cp;
private HangManPane hangManPane;

//...

public GamePane() {
    cp = new ControlPane();
    hangManPane = new HangManPane(null);

    cp.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent evt) {
            String secret = cp.getSecretWord();
            DefaultHangManModel model = new DefaultHangManModel(secret);
            hangManPane.setModel(model);
        }
    });
}

例如...

这篇关于使用ChangeListener触发Java Swing中的更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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