我是否正确使用 actionlistener

am i using actionlistener properly

我想了解为什么这个程序不起作用。我是否正确使用了 actionlistener 为什么它不显示线程为 运行 但线程不是 运行 时经过的时间并且我在屏幕上看不到任何内容。 我做错了什么或者这是使用 ActionListener 的错误方式 class 还是我没有正确使用它,这是所有文件的代码。

import java.awt.event.*;
import java.util.TooManyListenersException;

public class Timer extends Thread {
    private int iTime = 0;
    private ActionListener itsListener = null;

    public Timer() {
        super(); // allocates a new thread object
        iTime = 0; // on creation of this object set the time to zero
        itsListener = null; // on creation of this object set the actionlistener
                            // object to null reference
    }

    public synchronized void resetTime() {
        iTime = 0;
    }

    public void addActionListener(ActionListener event) throws TooManyListenersException {
        if (event == null)
            itsListener = event;
        else
            throw new TooManyListenersException();
    }

    public void removeActionListener(ActionListener event) {
        if (itsListener == event)
            itsListener = null;
    }

    public void run() {

        while (true) {
            try {
                this.sleep(100);
            } 
            catch (InterruptedException exception) {
                // do nothing
            }
            iTime++;

            if (itsListener != null) {
                String timeString = new String(new Integer(iTime).toString());

                ActionEvent theEvent = new ActionEvent(this,
                        ActionEvent.ACTION_PERFORMED, timeString);

                itsListener.actionPerformed(theEvent);
            }
        }
    }
}

下一个文件

import java.awt.event.*;
import java.util.TooManyListenersException;

public class TextTimeDemonStration extends Object implements ActionListener {
    private Timer aTimer = null; // null reference for this object

    public TextTimeDemonStration() {
        super();

        aTimer = new Timer();

        try {
            aTimer.addActionListener(this);
        } 
        catch (TooManyListenersException exception) {
            // do nothing
        }

        aTimer.start();
    }

    public void actionPerformed(ActionEvent e) {
        String theCommand = e.getActionCommand();

        int timeElapsed = Integer.parseInt(theCommand, 10);

        if (timeElapsed < 10) {
            System.out.println(timeElapsed);
        }
        else
            System.exit(0);
    }
}

最后一个文件主要class运行来自

的程序
public class MainTest {
    public static void main(String[] args) {
        TextTimeDemonStration test = new TextTimeDemonStration();    
    }    
}

TextTimeDemonStration class 中,您调用了 aTimer.addActionListener(this); 来分配一个动作侦听器

但是在你的 Timer.addActionListener() 方法中,在 if else 块中,你写了

if (event == null)
    itsListener = event;
else
    throw new TooManyListenersException();

因为 this 不为空,它会抛出 TooManyListenersException 会被捕获的异常。

之后您开始计时。但是,由于 itsListener 在初始化后为 null,因此在您的 Timer.run() 方法中,永远不会执行 if 块。因此,什么也没做。

修复 Timer.addActionListener() 方法中的逻辑,它应该可以正常工作。

希望这对您有所帮助。