java计时器怎么插入,在我的程序中添加一个计时器(javafx)

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{

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.

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.

解决方案

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

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.

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..

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..

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

void schedule(TimerTask task,long milliseconds)

For further reference

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值