在我的程序中添加一个计时器 (javafx)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35512648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding a timer to my program (javafx)
提问by Dennis Lukas
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.
我正在尝试向我的程序添加一个计时器。我试过调用 java.util.Timer 但它让我感到沮丧,因为我不完全理解它背后的概念。我刚刚用 python 完成了一个学期的编码,但这对我来说是一个完全不同的水平。
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();
}
}
}
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.
我在 python 中构建了一个带有主循环的程序。我正在考虑通过在循环中添加一个检查来检查当前日期的秒数是否已更改,如果已更改,则将值增加 1 来执行此操作的“脏”方式。
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.
我该怎么办?很抱歉给您带来不便。
采纳答案by Vaibhav G
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
用于调度任务..那么你在哪里写这些任务??那么你必须在TimerTask
课堂上写这些任务......
Confused ? Let me break it down,
使困惑 ?让我分解一下,
Timer timer = new Timer();
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
.
现在你已经创建了一个 Timer 类的对象..现在你必须做一些任务吗?所以要做到这一点,请创建一个TimerTask
.
TimerTask task = new TimerTask()
{
public void run()
{
//The task you want to do
}
};
Now you have created a task where you should be defining the tasks you want to do inside the run method..
现在您已经创建了一个任务,您应该在其中定义要在 run 方法中执行的任务。
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..
为什么我自己写了一个 TimerTask 类?那是因为 TimerTask 是一个具有三个方法的抽象类..所以你需要定义你想要使用的任何方法..
Now scheduling the task
现在安排任务
timer.schedule(task,5000l);
Note the 'l' after 5000. It is to denote long data type because the schedule
method is defined as
注意5000后的'l'。它表示长数据类型,因为该schedule
方法定义为
void schedule(TimerTask task,long milliseconds)
void schedule(TimerTask task,long milliseconds)
For further reference
供进一步参考
https://docs.oracle.com/javase/7/docs/api/java/util/Timer.html
https://docs.oracle.com/javase/7/docs/api/java/util/Timer.html
https://docs.oracle.com/javase/7/docs/api/java/util/TimerTask.html
https://docs.oracle.com/javase/7/docs/api/java/util/TimerTask.html