将计时器添加到我的程序 (javafx)
Adding a timer to my program (javafx)
我想在我的程序中添加一个计时器。我试过调用 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();
}
}
}
我在 python 中构建了一个带有主循环的程序。我正在考虑一种 "dirty" 方法,通过向循环添加一个检查来检查当前日期的秒数是否已更改,如果已更改,则将值递增 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;
}
}
这是我创建新计时器的进度。我知道我必须创建一个任务并将其安排到计时器,但我不知道如何...
我该怎么做?抱歉给您带来不便。
Timer
用于调度任务..那么你把那些任务写在哪里??那么你必须将这些任务写在 TimerTask
class...
困惑?让我分解一下,
Timer timer = new Timer();
现在您已经创建了一个定时器对象 class ..
现在你必须做一些任务对吗?所以要创建一个 TimerTask
.
的对象
TimerTask task = new TimerTask()
{
public void run()
{
//The task you want to do
}
};
现在您已经创建了一个任务,您应该在其中定义要在 运行 方法中执行的任务..
为什么我自己写了一个 TimerTask class?
那是因为 TimerTask 是一个具有三个方法的抽象 class..
所以你需要定义你想使用的方法..
正在安排任务
timer.schedule(task,5000l);
注意5000后面的'l',表示long数据类型,因为schedule
方法定义为
void schedule(TimerTask task,long milliseconds)
进一步参考
https://docs.oracle.com/javase/7/docs/api/java/util/Timer.html
https://docs.oracle.com/javase/7/docs/api/java/util/TimerTask.html
我想在我的程序中添加一个计时器。我试过调用 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();
}
}
}
我在 python 中构建了一个带有主循环的程序。我正在考虑一种 "dirty" 方法,通过向循环添加一个检查来检查当前日期的秒数是否已更改,如果已更改,则将值递增 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;
}
}
这是我创建新计时器的进度。我知道我必须创建一个任务并将其安排到计时器,但我不知道如何...
我该怎么做?抱歉给您带来不便。
Timer
用于调度任务..那么你把那些任务写在哪里??那么你必须将这些任务写在 TimerTask
class...
困惑?让我分解一下,
Timer timer = new Timer();
现在您已经创建了一个定时器对象 class ..
现在你必须做一些任务对吗?所以要创建一个 TimerTask
.
TimerTask task = new TimerTask()
{
public void run()
{
//The task you want to do
}
};
现在您已经创建了一个任务,您应该在其中定义要在 运行 方法中执行的任务..
为什么我自己写了一个 TimerTask class? 那是因为 TimerTask 是一个具有三个方法的抽象 class.. 所以你需要定义你想使用的方法..
正在安排任务
timer.schedule(task,5000l);
注意5000后面的'l',表示long数据类型,因为schedule
方法定义为
void schedule(TimerTask task,long milliseconds)
进一步参考
https://docs.oracle.com/javase/7/docs/api/java/util/Timer.html
https://docs.oracle.com/javase/7/docs/api/java/util/TimerTask.html