简体   繁体   中英

How to use TimerTask in an auto generated JFrame?

I used netbeans to generate a JFrame and I want to add a TickTimer to it (A count down from 10 to 1), my question is: where do I add my input for such a thing? and where do I add the static class in which I'll extend the TimerTask class? in the main or in the constructor before the initComponents() or with the fields? I might be getting it wrong so if you could write me a little simple program that does this even if you do it with Timer not TimerTask I would be thankful.

After going through some articles finally found the answer and here you go if you have the same problem (Making a count down timer using Timer:

public class example extends JFrame implements ActionListener{
    javax.swing.Timer timer = new javax.swing.Timer(1000,  this);
    int c = 10;

    public example(){
        timer.start();
        initComponents(); // this is auto generated when creating JFrame

    @Override
    public void actionPerformed(ActionEvent e) {
        jLabel1.setText(""+c--);
        if (c == -1) {
            timer.stop();
        } 
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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