java changelistener,使用ChangeListener触发Java Swing中的更改?

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

ControlPane should fire notifications when the user clicks "New Game" button.so this fireChange should happen in the ActionListener of the New Game button.

GamePane listens to the notifications and pass the information to HangManPane

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);

}

}

xoQDM.png

解决方案

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

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

public void addChangeListener(ChangeListener listener) {

listenerList.add(ChangeListener.class, listener);

}

You'll need a remove method

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);

}

}

}

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();

}

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);

}

});

}

For example...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java的监听器(Listener)是一种广泛应用的设计模式,它用于处理程序的事件。通过监听器,我们可以在事件发生时执行一些特定的操作。在Java,我们可以使用内置的监听器API或自定义监听器实现此功能。 下面是Java的常见监听器类型: 1. ActionListener:用于处理按钮、菜单等组件的动作事件。 2. WindowListener:用于处理窗口事件,如窗口打开、关闭等。 3. MouseListener:用于处理鼠标事件,如单击、双击、拖动等。 4. KeyListener:用于处理键盘事件,如键盘按下、释放等。 5. FocusListener:用于处理组件焦点事件,如获得或失去焦点等。 使用监听器的步骤如下: 1. 创建一个监听器类,该类实现了相应的监听器接口。 2. 在需要监听的组件上添加监听器对象。 3. 在监听器类实现相应的方法来处理事件。 下面是一个简单的示例代码,演示了如何使用ActionListener监听器处理按钮单击事件: ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ButtonListenerDemo implements ActionListener { private JFrame frame; private JButton button; public ButtonListenerDemo() { frame = new JFrame("Button Listener Demo"); button = new JButton("Click Me"); button.addActionListener(this); frame.getContentPane().add(button); frame.pack(); frame.setVisible(true); } public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(frame, "Button Clicked!"); } public static void main(String[] args) { new ButtonListenerDemo(); } } ``` 在上面的代码,我们创建了一个ButtonListenerDemo类,该类实现了ActionListener接口。在构造函数,我们创建了一个按钮对象,然后将该按钮添加到窗口,并将该按钮的监听器设置为当前类。当用户单击按钮时,程序将调用actionPerformed()方法来处理事件,该方法将弹出一个消息框来告诉用户按钮已被单击。 总之,监听器是Java编程非常重要的组成部分。使用监听器,我们可以轻松地处理程序的事件,并实现交互式用户界面。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值