使用JButton启动计时器

时间:2013-12-28 03:49:09

标签: java swing jbutton

我想知道如何在点击JButton时启动'计时器'。我环顾四周,看看是否有与我的情况相关的答案,我找不到答案。

以下是我之前提问的代码示例:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GreenRects extends JPanel {

    private static final int SCREEN_WIDTH = 500;
    private static final int SCREEN_HEIGHT = 500;

    private static final int OFFSET = 100;
    private static final int SIZE = 20;
    private static final int INC = 5;

    int[] xPoints = new int[5];

    public GreenRects() {
        int x = 0;
        for (int i = 0; i < xPoints.length; i++) {
            xPoints[i] = x;
            x += OFFSET;
        }

        Timer timer = new Timer(50, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                for (int i = 0; i < xPoints.length; i++) {
                    if (xPoints[i] + INC < SCREEN_WIDTH) {
                        xPoints[i] += INC;
                    } else {
                        xPoints[i] = 0;
                    }
                }

                repaint();
            }
        });
        timer.start();
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.setColor(Color.red);
        g.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);

        g.setColor(Color.green);
        for (int i = 0; i < xPoints.length; i++) {
            g.fillRect(xPoints[i], 0, SIZE, SIZE);
        }
    }

    public Dimension getPreferredSize() {
        return new Dimension(SCREEN_WIDTH, SCREEN_HEIGHT);
    }

    public static void createAndShowGui() {
        JFrame frame = new JFrame();
        frame.add(new GreenRects());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);

    }

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

2 个答案:

答案 0 :(得分:4)

首先,将timer设为实例变量......

    private static final int SCREEN_WIDTH = 500;
    private static final int SCREEN_HEIGHT = 500;

    private static final int OFFSET = 100;
    private static final int SIZE = 20;
    private static final int INC = 5;

    int[] xPoints = new int[5];
    // Add me here...
    private Timer timer;

不要忘记删除旧的声明......

Timer timer = new Timer(50, new ActionListener() {

应该成为

timer = new Timer(50, new ActionListener() {

否则,您最终会得到一个阴影变量,并且可能会NullPointerException

接下来,创建一个JButton并向其中添加ActionListener,然后在其中调用timer.start()

JButton btn = new JButton("Start");
btn.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        timer.start();
    }
});

现在,不要忘记将其添加到您的UI,以便用户可以单击它...

请查看How to use buttons了解详情

<强>更新

根据评论,我会向start课程添加stop甚至GreenRects方法。这些会调用timer ....

public void start() {
    timer.start();
}

从那里,当触发按钮ActionListener时,您只需调用start实例的GreenRects方法...

public void actionPerformed(ActionEvent e) {
    greenRects.start();
}

然后我会将按钮添加到其他容器中,这样就不会打断GreenRects类正在绘制的内容......

答案 1 :(得分:1)

您正在创建一个ActionListener,但您没有任何可听的内容。幸运的是,JButton通过其AbstractButton超类具有addAction方法。通常通过其具体实现Action创建AbstractAction实例,并将其添加到按钮。 Action优于ActionListener的优势在于您也可以将其添加到JMenuItem