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

这篇博客探讨了在JavaFX程序中添加计时器的挑战。作者尝试使用`java.util.Timer`但感到困惑,因为其背景仅限于Python编程。文章提供了JavaFX界面的基本布局,并展示了如何通过按钮触发计数器的重置和递增操作。作者寻求将计时器与日期秒数同步的方法,希望通过定时任务每秒更新计数值。博客以对`Timer`和`TimerTask`的理解为基础,讨论了如何在JavaFX应用中实现这个功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值