- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个涉及计时器的小程序,但由于某种原因我无法让 TimerTask 来更新 Label.setText 的值
public class Main extends Application {
Label TimerLabel;
/* Create a Button named button */
Button button;
/* Create three Radiobuttons named Radio1,Radio2,Radio3 */
RadioButton Radio1, Radio2, Radio3;
Timer QuestionTimer = new Timer();
TimerTask QuestionTick = new TimerTask() {
@Override
public void run() {
TimerLabel.setText(String.valueOf(Integer.valueOf(TimerLabel.getText())+1));
}
};
public static void main(String[] args) {
launch(args);
}
/*UI Part*/
@Override
public void start(Stage primaryStage) throws Exception {
/*Window(Stage) Title is set to "Try 1"*/
primaryStage.setTitle("Try 1");
/*Button properties*/
button = new Button();
button.setText("Click Me");
//Radio Button
Radio1 = new RadioButton();
Radio1.setText("Click Me 1");
Radio1.setOnAction(e->{
System.out.println("Radio Button Clicked");
});
Radio2 = new RadioButton();
Radio2.setText("Click Me 2");
Radio2.setOnAction(e->{
System.out.println("Radio Button 2 Clicked");
});
Radio3 = new RadioButton();
Radio3.setText("Click Me 3");
Radio3.setOnAction(e->{
System.out.println("Radio Button 3 Clicked");
});
TimerLabel = new Label();
TimerLabel.setText("0");
/*Here my layout is defined */
Pane Buttonlayout = new Pane();
button.setLayoutX(200);
button.setLayoutY(200);
Radio1.setLayoutX(15);
Radio1.setLayoutY(20);
Radio2.setLayoutX(15);
Radio2.setLayoutY(40);
Radio3.setLayoutX(15);
Radio3.setLayoutY(60);
TimerLabel.setLayoutX(100);
TimerLabel.setLayoutY(20);
Buttonlayout.getChildren().add(button);
Buttonlayout.getChildren().add(Radio1);
Buttonlayout.getChildren().add(Radio2);
Buttonlayout.getChildren().add(Radio3);
Buttonlayout.getChildren().add(TimerLabel);
/*Here we define the scene (aka everything inside the stage (inside the window))*/
Scene scene1 = new Scene(Buttonlayout,300,250);
primaryStage.setScene(scene1);
primaryStage.show();
QuestionTimer.scheduleAtFixedRate(QuestionTick,1000,1000);
}
}
这就是我的代码,我知道其中大部分看起来很愚蠢,但我首先想开始在计时器上编程,但它不起作用。任何形式的帮助将不胜感激
最佳答案
从代码中可以看出您正在使用java.util.Timer
类。文档指出实现说明:所有构造函数都会启动一个计时器线程。
JavaFX UI 不应从另一个线程更新,而这正是您对计时器所做的事情。
不要直接更新 UI,而是使用 Platform.runLater(Runnable)在主 JavaFX 线程上安排 UI 任务。
javafx.application.Platform.runLater(new Runnable() {
@Override
public void run() {
TimerLabel.setText(String.valueOf(Integer.valueOf(TimerLabel.getText())+1));
}
}
请帮自己一个忙,摆脱方法链。当 Integer.valueOf
抛出 Exception
时,它将使调试变得更容易。
String timer_label = TimerLabel.getText();
Integer timer_int = Integer.valueOf(timer_label);
String timer_text = String.valueOf(timer_int + 1);
TimerLabel.setText(timer_text);
关于java - 我的 TimerTask 函数无法更新 label.setText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37161831/
我正在用 Java 编写应用程序,但遇到以下问题:一段时间后,显示了一个 inputDialog(JOptionPane)。因为间隔很短,即 10 秒,如果我延迟与第一个对话的交互,那么第二个将在我之
基本上我想做的是制作一个计时器,它在 x 秒后运行一个特定的 TimerTask,但是 TimerTask 可以重新安排计时器在 y 秒后执行任务。下面是示例,它在我尝试在 TimerTask 运行中
在下面的代码中,注释掉的部分会导致异常,该异常向我们解释了 timerTask2 已经被调度或取消,但是没有什么可以阻止 if 被执行器在下面的代码中重用和运行注释掉了部分。怎么解释呢? Timer
我编写了一个连接到服务器并向他发送 ping 命令的应用程序,服务器使用 pong 命令进行应答。 我想实现连接超时机制。我认为它将如下: 客户端发送 ping 并使用 timertask 和 del
我遇到以下问题。每隔 2 秒,程序就会进入 if 语句。在这个 if 语句中,我想要一个计时器,它会在 15 秒后给我一条消息。计时器应延迟 1 秒运行。但是当我用计时器“等待”时,if 语句将再执行
我正在开发一个 ODL 应用程序,并从我的主函数中安排了一个任务 ( MonitorLinksTask ),以便监视拓扑的所有链接中的延迟。 我想为每个链接发送一个数据包,并在 Controller
尝试使用 ScheduledExecutorService 来执行 TimerTask。安排一个延迟 10 秒的任务,并调用 task.cancel。但任务仍在运行,不确定会发生什么,并且取消方法似乎
无论如何,我正在尝试制作类似聊天程序的东西,有人告诉我使用此代码来检查新消息,同时允许用户提交消息: timer.schedule(new TimerTask() { @Override pu
看看这段代码: public Reminder() { a[0]=1000; a[1]=3000; a[2]=1000; a[3]=5000; timer = n
我有以下设计: 有一个任务扩展了TimerTask,并且计划每分钟运行一次。此任务将尝试从中央队列(作为单个消费者)获取项目并将其表示形式写入文件中。 此外,还有多个生产者不时将项目放入中央队列。 我
我在打印特定数组的每个值(在一定时间内)时遇到问题。例如,我有一个包含值的数组:“Value1”、“Value2”、“Value3”。我想输出“Value1”,5秒后“Value2”,5秒后“Valu
我对这个标题感到抱歉,但我不确定还有什么方法可以描述这个问题。我有一个 SingletonBean,其中有一个扩展 TimerTask 的类。要点是,当应用程序启动时,它开始循环运行一组计划的后台任务
我有一个程序,它启动一个计时器,然后在停止之前每秒重复一次代码。发生的情况是它不会停止来自 main 的执行。我假设它只是一个与之一起运行的线程,但通过查看帖子,我仍然不确定如何暂时停止它。 fina
我正在尝试使用每秒更新标签的时间(因此它显示倒计时)但它只似乎“滴答作响”一次,我无法弄清楚我做错了什么! public class Puzzle extends UiApplication {
我设置了 TimerTask UpdateTask,但它只在我启动程序时触发一次。为什么不继续触发? 这里的一些方法在其他类中,如果您需要它们,请随时告诉我。 import java.awt.Grap
我又碰壁了。让我的键输入工作后,我已经绞尽脑汁几个小时,我想创建一个暂停功能,这样如果再次按下同一个键,timertask 将停止运行(即游戏暂停) JPanel component = (JPane
我有一个 android 应用程序,我必须在 20-30 秒的非常短的时间间隔内执行一些任务。这是一项要求,它在某种程度上是一个“监控”应用程序,不会用于商业目的,因此无需担心电池和数据使用。 我正在
这是我的计时器类,该类旨在不断更新 View 中的计时器。但是,当我运行该应用程序时,第一条 toast 消息显示在屏幕上,但从未到达第二条消息(永远不会执行 timerTask 的“运行”方法)。我
我在我的android代码中添加了一个TimerTask: private class MyTimerTask extends TimerTask { @Override public
我有一个名为 timer 的任务: timer.schedule(new task1(), 1000*minutes); 任务: class task1 extends TimerTask {
我是一名优秀的程序员,十分优秀!