在我的程序中添加一个计时器(javafx) [英] Adding a timer to my program (javafx)

查看:821
本文介绍了在我的程序中添加一个计时器(javafx)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的程序添加一个计时器。我试过调用java.util.Timer但它让我感到沮丧,因为我不完全理解它背后的概念。我刚刚在python中完成了一个学期的编码,但这与我完全不同。

I am trying to add a timer to my program. I've tried calling upon java.util.Timer but it frustrates me as i do not completely understand the concepts behind it. I have just done a semester of coding in python but this is an entirely different level to me.

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import java.awt.event.ActionListener;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class main extends Application implements EventHandler<ActionEvent>{

    Button button;
    Button button2;
    Counter counter = new Counter(0);
    Counter timecounter = new Counter(0);
    Text text_counter = new Text(counter.S_count.get());
    Text text_timecounter = new Text(timecounter.S_count.get());

    Date currdate = new Date();
    int currsec = currdate.getSeconds();


    public static void main(String[] args) {
        launch(args);


    }


    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("Counter Window");

        button = new Button();
        button2 = new Button();
        button.setText("Reset");
        button.setOnAction(this);
        button2.setText("Tick");
        button2.setOnAction(this);
        button.setTranslateY(-120);
        button2.setTranslateY(-80);
        text_counter.textProperty().bind(counter.S_count);
        text_timecounter.textProperty().bind(timecounter.S_count);
        text_timecounter.setTranslateX(-100);



        StackPane layout = new StackPane();
        layout.getChildren().add(button);
        layout.getChildren().add(button2);
        layout.getChildren().add(text_counter);
        layout.getChildren().add(text_timecounter);

        Scene scene = new Scene(layout, 360,280);
        primaryStage.setScene(scene);

        primaryStage.show();
    }

    @Override
    public void handle(ActionEvent event) {
        if(event.getSource()==button){
            counter.Reset();
        }
        if(event.getSource()==button2){
            counter.Tick();
        }
    }
}

我在python中构建了一个程序有一个主循环。我想通过在循环中添加一个检查脏方法来检查当前日期的秒数是否已经改变,如果有,则将值增加1。

I built a program in python with a main loop. I was thinking of a "dirty" way of doing it by adding a check to the loop that checks if the seconds of the current date has changed and if it has, increment a value by one.

class timerclass {
    Timer timer1;
    private int timecounter = 0;

    TimerTask Task1 = new TimerTask() {
        @Override
        public void run() {
            setTimecounter(getTimecounter()+1);
        }
    };

    public timerclass(){
        timer1 = new Timer();

    }

    public int getTimecounter() {
        return timecounter;
    }

    public void setTimecounter(int timecounter) {
        this.timecounter = timecounter;
    }
}

这是我创造一个新的目标计时器。我明白我必须创建一个任务并安排它到计时器,但我没有丝毫的线索如何......

This is how far i've come creating a new timer. I understood that I have to create a task and schedule it to the timer but I don't have the slightest clue how...

我将如何解决这个问题?对不起,不好意思。

How would I go about this? Sorry for the inconvieniences.

推荐答案

计时器用于安排任务 ..那你在哪里写这些任务?那么你必须在 TimerTask 类中编写这些任务...

Timer is used to schedule tasks.. So where do you write those tasks?? Well you have to write those tasks in a TimerTask class...

困惑?让我分解一下,

Timer timer = new Timer();

现在你已经创建了一个Timer类的对象..
现在你必须要做一些任务对吗?所以要创建一个 TimerTask 的对象。

Now you have created a object of Timer class .. Now you have to do some task right? So to do that create an object of TimerTask.

TimerTask task = new TimerTask()
{
        public void run()
        {
            //The task you want to do       
        }

};

现在您已经创建了一个任务,您应该在run方法中定义要执行的任务..

Now you have created a task where you should be defining the tasks you want to do inside the run method..

为什么我自己写了一个TimerTask类?
那是因为TimerTask是一个带有三种方法的抽象类..
所以你需要定义你想要使用的方法..

Why have I written a TimerTask class itself?? Thats because TimerTask is a abstract class with three methods.. So you need to define whichever method you want to use..

现在安排任务

timer.schedule(task,5000l);

请注意5000之后的'l'。它表示长数据类型,因为 schedule 方法定义为

Note the 'l' after 5000. It is to denote long data type because the schedule method is defined as

void schedule(TimerTask任务,长毫秒) )

供进一步参考

https://docs.oracle.com/javase/7/docs/api/java/util/ Timer.html

https://docs.oracle.com/javase/7/docs/api/java/util/TimerTask.html

这篇关于在我的程序中添加一个计时器(javafx)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆