简体   繁体   English

如何在没有JButton的情况下使用actionListeners?

[英]How do I use actionListeners without JButtons?

I was reading the documentation on swing timers, when I came across some information about ActionListener s. 当我遇到有关ActionListener的一些信息时,我正在阅读有关摆动计时器的文档。 When further researched, all I could find is how to create an ActionListener attached to a JButton , etc. How can you create a plain ActionListener , not attached to anything? 经过进一步研究,我所能找到的就是如何创建一个附加到JButton等的ActionListener 。如何创建一个普通的ActionListener ,而不附加任何东西?

My timer is not working correctly, and I thought it may be because I was incorrectly using the ActionListener . 我的计时器无法正常工作,我认为可能是因为我不正确地使用了ActionListener

Here is my code: 这是我的代码:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;

public class MyTimer {

    ActionListener al = new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            System.out.println("testing");
        }
    };

    public MyTimer() {

        Timer timer = new Timer(10, al);
        timer.start();
    }

    public static void main(String[] args) {
        MyTimer start = new MyTimer();
    }
}

An ActionListener is just an interface 一个ActionListener只是一个interface

You can create an stand alone version by implementing it and then instanstanting it.... 您可以通过实施然后实例化来创建独立版本。

public class MyActionHandler implements ActionListener {
    public void actionPerformed(ActionEvent evt) {
        // do something...
    }
}

And sometime in the future... 还有将来的某个时候

MyActionHandler handler = new MyActionHandler();

Or you could create an anonymous instance.... 或者您可以创建一个匿名实例。

ActionListener al = new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
        // do something...
    }
};

Take a look at Interfaces for more details 查看接口以了解更多详细信息

How can you create a plain actionlistner, not attached to anything? 您如何创建一个普通动作列表,而不附加任何内容?

Loot at this: 抢劫:

ActionListener listener = new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        System.out.println("Hello World!");
    }
};

// Using the listener with 2 seconds delay
java.swing.Timer timer = new java.swing.Timer(2000, listener);
timer.setRepeats(false);

// Start the timer
timer.start();

Try with this: 试试这个:

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;

public class MyTimer {
    ActionListener al = new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            System.out.println("testing");
        }
    };

    public MyTimer() {
        Timer timer = new Timer(1000, al);
        timer.start();
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                 new MyTimer();
            }
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM