1

我需要在我的应用程序中实现一个计时器,它将从 10 秒 - 0 秒开始倒计时。并且,在 a 中显示倒计时JLabel

这是我的实现;

...
Timer t = new Timer(1000, new List());
        t.start();

}

class List implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        int sec = 0;
        label.setText(""+sec);
        // Do a if- condition check to see if the clock has reached to, and then stop

    }

}

我期待 JLabel 从 0 到 10 开始计数,然后停止。但事实并非如此。JLabel 设置值0并且它不会增加。

更新 1

    t = new Timer(1000, new Listner());
    t.start();


}

class Listner implements ActionListener{
    private int counter = 0;
    @Override
    public void actionPerformed(ActionEvent e) {
        lable.setText(""+ (counter++));

        if (counter == 10)
              t.removeActionListener(this);

    }

}
4

4 回答 4

4

好吧,每次调用计时器时,它都会将 int 变量 sec 声明为 0。因此标签不会被更新。

您应该将 sec 变量声明为全局变量,然后在 actionPerformed 方法中每次调用它时递增其值。

 public int sec = 0;
 class List implements ActionListener{

   @Override
   public void actionPerformed(ActionEvent e) {
        sec++;
        label.setText(""+sec);
        // Do a if- condition check to see if the clock has reached to, and then stop

  }

}
于 2012-12-10T17:09:06.497 回答
4

您没有secs在任何地方存储或增加,所以我看不到它应该如何更新,尝试使用

Timer timer;

void start() {
  timer = new Timer(1000,new List());
}

class List implements ActionListener {
    private counter = 0;
    @Override
    public void actionPerformed(ActionEvent e) {
        label.setText(""+counter++);

        if (counter == 10)
          timer.removeActionListener(this);
    }
}

请注意,您需要在某处存储对计时器的引用,以便在倒计时完成后能够从中删除侦听器。

于 2012-12-10T17:09:38.307 回答
3

一个完整的例子

public class ATimerExample {

    Timer timer;
    int counter = 0;

    public ATimerExample() {
        final JFrame frame = new JFrame("somethgi");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final JLabel label = new JLabel("0");
        JPanel panel = new JPanel();
        panel.add(label, BorderLayout.SOUTH);
        frame.getContentPane().add(panel);
        frame.pack();
        frame.setVisible(true);

        timer = new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                label.setText(String.valueOf(counter));
                counter++;
                if (counter == 10) {
                    //timer.removeActionListener(this);
                      timer.stop();
                }
            }
        });
        timer.start();
    }

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

            @Override
            public void run() {
                new ATimerExample();
            }
        });
    }
}
于 2012-12-10T17:35:30.423 回答
1

由于java以毫秒为单位读取时间,它应该是10000而不是1000。试试你的代码,看看它是否有效。当我想要 30 秒时,我遇到了同样的问题。而不是写 Timer t = new Timer(30000, new List()); t.start();

我写了 Timer t = new Timer(3000, new List()); t.start();

这使我的程序每 3 秒停止一次。我建议您使用 10000 而不是 1000。

记得做: t.stop() 在你的 List 类。谢谢

于 2017-04-28T01:51:16.060 回答