在Timer类中使用匿名侦听器对象

时间:2021-03-24 20:03:09

Can it be done? I tried doing this, but it gives a compilation error:

可以吗?我尝试这样做,但它给出了编译错误:

Timer t = new Timer(1000,new ActionListener() {
    public void actionPerformed(ActionEvent event) {

    }
});

Here's the entire code for reference

这是整个代码供参考


Full Code:

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class Scratch {


    public static void main(String[] args) {
        JFrame frame = new JFrame("Moving Rectangle");
        frame.setSize(1000,700);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new JComponent() {
            public void paintComponent(Graphics g) {
                Graphics2D g2 = (Graphics2D) g;

            }

        });
        Timer t = new Timer(1000,new ActionListener() {
            public void actionPerformed(ActionEvent event) {

            }
        });

    }
}

I need to type something as my question is mostly code.

我需要输入一些内容,因为我的问题主要是代码。

1 个解决方案

#1


but it gives a compilation error:

但它给出了编译错误:

When you ask a question post the error so we don't have to guess.

当你提出一个问题发布错误,所以我们不必猜测。

When I added your code to an empty main() method I got the following because I had a lot of standard import statements in the test class:

当我将代码添加到空的main()方法时,我得到以下内容,因为我在测试类中有很多标准的import语句:

Main.java:21: error: reference to Timer is ambiguous, both class java.util.Timer in java.util and class javax.swing.Timer in javax.swing match
Timer t = new Timer(1000, new ActionListener()

The solution can be to use:

解决方案可以是:

javax.swing.Timer t = new javax.swing.Timer(1000, new ActionListener()

to avoid confusion.

避免混淆。

Edit:

Did you look at my solution above? Notice how I'm using a javax.swing.Timer?

你看过我上面的解决方案了吗?请注意我是如何使用javax.swing.Timer的?

import java.util.Timer;

Don't use java.util.Timer. With Swing you need to use the Swing Timer, so the code is executed on the EDT.

不要使用java.util.Timer。使用Swing,您需要使用Swing Timer,因此代码在EDT上执行。

Instead use:

import javax.swing.Timer;

#1


but it gives a compilation error:

但它给出了编译错误:

When you ask a question post the error so we don't have to guess.

当你提出一个问题发布错误,所以我们不必猜测。

When I added your code to an empty main() method I got the following because I had a lot of standard import statements in the test class:

当我将代码添加到空的main()方法时,我得到以下内容,因为我在测试类中有很多标准的import语句:

Main.java:21: error: reference to Timer is ambiguous, both class java.util.Timer in java.util and class javax.swing.Timer in javax.swing match
Timer t = new Timer(1000, new ActionListener()

The solution can be to use:

解决方案可以是:

javax.swing.Timer t = new javax.swing.Timer(1000, new ActionListener()

to avoid confusion.

避免混淆。

Edit:

Did you look at my solution above? Notice how I'm using a javax.swing.Timer?

你看过我上面的解决方案了吗?请注意我是如何使用javax.swing.Timer的?

import java.util.Timer;

Don't use java.util.Timer. With Swing you need to use the Swing Timer, so the code is executed on the EDT.

不要使用java.util.Timer。使用Swing,您需要使用Swing Timer,因此代码在EDT上执行。

Instead use:

import javax.swing.Timer;