Java 倒数

Java 倒数,java,swing,timer,jlabel,Java,Swing,Timer,Jlabel,我需要在我的应用程序中实现一个计时器,它将从10秒到0秒倒计时。 并且,在JLabel中显示倒计时 这是我的实现 ... Timer t = new Timer(1000, new List()); t.start(); } class List implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { int sec = 0;

我需要在我的应用程序中实现一个计时器,它将从10秒到0秒倒计时。 并且,在
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);

    }

}

每次调用计时器时,它都会将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

  }

}

每次调用计时器时,它都会将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

  }

}

您没有在任何地方存储或增加
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);
    }
}

请注意,您需要在某个位置存储对计时器的引用,以便在倒计时结束后将侦听器从中删除。

您没有在任何位置存储或递增
秒,因此我看不出它应该如何更新,请尝试使用

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);
    }
}
请注意,您需要将对计时器的引用存储在某个位置,以便在倒计时结束后将侦听器从中删除。

一个完整的示例

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();
            }
        });
    }
}
一个完整的例子

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();
            }
        });
    }
}

因为java以毫秒为单位读取时间,所以它应该是10000而不是1000。试试你的代码,看看是否有效。当我需要30秒的时候,我也有同样的问题。而不是写入计时器t=new Timer(30000,new List());t、 start()

我编写了Timer t=newtimer(3000,newlist()); t、 start()

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


记住在List类中执行:t.stop()。谢谢

因为java以毫秒为单位读取时间,所以它应该是10000而不是1000。试试你的代码,看看是否有效。当我需要30秒的时候,我也有同样的问题。而不是写入计时器t=new Timer(30000,new List());t、 start()

我编写了Timer t=newtimer(3000,newlist()); t、 start()

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


记住在List类中执行:t.stop()。谢谢

我建议交换顺序并将
sec
作为listener类的一个属性。例如,
类列表实现ActionListener{private int sec=0;。
计数器不是从
0
开始,而是从
8
开始,然后在9结束。这是因为我在
10
停止计时器。我建议交换侦听器类的一个属性order&make
sec
。例如
类列表实现ActionListener{private int sec=0;。
计数器不是从
0
开始,而是从
8
开始,然后在9结束。这是因为我在
10
停止计时器。计数器不是每次调用执行的操作时都从0开始吗?@Fyre No.
private counter=0;
是在创建类时创建的。在
actionPerformed
中,
counter
刚刚递增。它也是
private
int
counter=0
?我做了更改,但它仍然没有递增。我看到了文本
9
。它没有递增或递减。但是您是否更改了actionPerformed中的代码以使用
counter
而不是你的局部变量?我用代码更新了我的帖子。我没有看到计数器的增量。Help不是每次调用actionPerformed时计数器都从0开始吗?@Fyre No.
private counter=0;
是在类创建时创建的。在
actionPerformed
中,
计数器
只是增量而已。它也是这样吗de>private
int
counter=0
?我做了更改,但它仍然没有递增。我看到文本
9
。它没有递增或递减。但是您是否更改了actionPerformed中的代码以使用
counter
而不是您的局部变量?我用代码更新了我的帖子。我没有看到计数呃,救命